Question

In: Computer Science

What to do: 1. Write a Python program, a1.py with the following characteristics: a) there are...

What to do:

1. Write a Python program, a1.py with the following characteristics:
a) there are at least 40 lines of comments and 40 lines of code (1 point)
b) the problem is described at the top of the program in a readable fashion (1 point)
c) the code includes at least one instance of each of the five learning objectives listed above. (3 points) d) the code solves the problem. This may be assessed in part by a 'code walkthrough' where you explain

your code to the TA. (4 points)
2. Run your program and collect the output in a text file called output.txt (1 point)

II have to make my own question and solve it through by making a program. This is my assignment but i haven't covered the chapters of my course yet and hence i don't have the knowledge to do this assignment within a short span of time. It can be any simple program but all the requirements should be met given above. Also please note, 20 comments will also work instead of 40 but 20 comments is a must for the program describing what the program is about. The chapters which were covered before this assignment was given were the following: 1) Introduction through python 2) Some simple numerical problems 3) Functions, Scoping and Abstractions from the book Introduction to Computation and Programming Using Python by John V. Guttag.

Please note, the program shouldn't be copied from anywhere and should be unique. PLEASE MAKE THE CODE UNIQUE OR ELSE I WILL BE CHARGED WITH PLAGIARISM.

Solutions

Expert Solution

PYTHON PROGRAM

Program to play Guess an integer and Guess an alphabet using built-in function and user defined functions. The explanation is given as comments.

import random
#For tracking total number of Games Played
total_games_played=0
#For tracking number of Games won
total_win=0
"""
Guess an integer is a game played with computer
The computer asks the user to choose the range
The computer randomly chooses a number in that range
The computer asks the player how many attempts they need to guess an integer
Read the guess and compare with the random number chosen by the computer
If the guess given by the player is high, display message that the guess is high
If the guess given by the player is low, display message that the guess is low
If the guess given by the player is right, print that the player wins
If the user has not guessed it right and still they have attempts,continue
Else if the number of attempts has exceeded, print that the player loses
"""
def guess_an_integer():
    print("Welcome to Guess an Integer Game")
    a=int(input("Enter the lower limit:"))
    b=int(input("Enter the upper limit:"))
    # System generates a random number in the range a to b
    number=random.randint(a,b)
    # Read the attempt limit from user
    attempt_limit=int(input("Number of Attempts you need to find the number:"))
    #Initialize attempt count to 1
    count=1
    #Get a number from user
    guess=int(input("Enter your guess"))
    #When the user makes an incorrect guess
    while(guess!=number):
        count=count+1
        #In case of high guess than the random number
        if(guess>number):
            print("Guess is high")
        #In case of low guess than the random number
        else:
            print("Guess is low")
        #If the limit exceeds the number of attempts
        if (count > attempt_limit): 
            print("GAME OVER. Incorrect guess! It is",number)
            #Exit the loop. Stop asking for further guess!
            break
        #If the user hasn't found the number and still has attempts left
        guess=int(input("Enter your guess"))
    #When the user guesses the right value
    else:  
        print("You win! You guessed it right in turn number",count)
        #Returns one if user wins the game
        return 1
    #Returns zero if user loses the game
    return 0
def guess_an_alphabet():
    """
    Guess an Alphabet is similar to Guess an Integer but it is guessing an alphabet in capital letter
    """
    print("Welcome to Guess an Alphabet Game!")
    #Since the ASCII equivalent of alphabets in upper case varies from 65 to 90, randomly generate in that range
    number=random.randint(65,90)
    #Convert that into a character
    computer_alphabet=chr(number)
    #Get the number of attempts from user
    attempt=int(input("Enter the number of Attempts you need:"))
    #Get a Capital Letter as Guess from User
    guess=input("Enter your Guess in Capital:")
    # Track number of Attempts in count
    count=1
    # If the Guess is incorrect
    while(guess!=computer_alphabet):
        # Increment the number of guess, if there are still attempts pending, get another alphabet
        count=count+1
        #If attempts exceeded, print game over and return 0 because the user has lost
        if count>attempt:
            print("GAME OVER! Attempts exceeded!It is",computer_alphabet)
            return 0
        guess=input("Incorrect! Please Guess Another Alphabet in capital")
    else:
        #If the user guessed it right, return 1
        return 1
while(1):
    print("What game do you want to play? Guess an Integer?? Guess an Alphabet?? ")
    print("Press 1 for Guess an Integer ; 2 for Guess an Alphabet ; Any other for Exit")
    choice=int(input())
    #If the user wants to guess an integer
    if(choice==1):
        #Play guess an integer,Track the total number of games played and number of games won
        total_win+=guess_an_integer()
        total_games_played+=1
    #If the user wants to guess an alphabet
    elif choice==2:
        #Play guess an alphabet,Track the total number of games played and number of games won
        total_win+=guess_an_alphabet()
        total_games_played+=1
    else:
    #When the user's input is for exit
        print("Total Games Played:",total_games_played)
        print("Games Won",total_win)
        break
        

        

SCREENSHOT OF PROGRAM TO ASSIST IN INDENTATION

OUTPUT SCREENSHOT


Related Solutions

What to do: 1. Write a Python program, a1.py with the following characteristics: a) there are...
What to do: 1. Write a Python program, a1.py with the following characteristics: a) there are at least 40 lines of comments and 40 lines of code (1 point) b) the problem is described at the top of the program in a readable fashion (1 point) c) the code includes at least one instance of each of the five learning objectives listed above. (3 points) d) the code solves the problem. This may be assessed in part by a 'code...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers from the user until they enter a 0 and computes the product of all these numbers and outputs it. Hint: use the example from the slides where we compute the sum of a list of numbers, but initialize the variable holding the product to 1 instead of 0. print("Enter n") n = int(input()) min = n while n != 0: if n < min:...
Write a Python program (with comments) to do the following: Assign the value '2+1' to a...
Write a Python program (with comments) to do the following: Assign the value '2+1' to a variable str1. Use the eval() function to evaluate str1and assign the result to a variable num1. Ask the user to input their birth month and assign it to a variable month and then print it with a suitable message. Determine the number of letters in month and display the result with a suitable message. Determine how many times the letter 'r ' occurs in...
IN PYTHON Write a program to do the following: Solve the a set of equations as...
IN PYTHON Write a program to do the following: Solve the a set of equations as mentioned in "Zybook 5.20 LAB: Brute force equation solver". Instead of the arithmetic operators use your own function defined in a module named calc. You must provide two files (calc.py, brute_force_solver.py) :- 1) calc.py:- Add function named 'add' instead of using operator '+' [10pts] Add function named 'difference' instead of using operator '-' [10pts] Add function named 'product' instead of using operator '*' [10pts]...
Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user...
Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user on one line (digits are separated by white space) and adds them to a list. The first digit and the last digit should not be a zero. If the user provides an invalid entry, the program should prompt for a new value. Converts every entry in the list to an integer and prints the list. The digits in the list represent a non-negative integer....
By only importing multiprocessing Write a python program which do the following : 1- Create one...
By only importing multiprocessing Write a python program which do the following : 1- Create one process this process must-read the content of a file “read any file content exist in your machine” and after that create another process in the first process function which will get the file content as a parameter for the second process function. 2- The second process function will get the file content and check out if there are any special characters or numbers in...
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
***This program is to be created using Python*** Requirements to be numbered in program01.py: Print “This...
***This program is to be created using Python*** Requirements to be numbered in program01.py: Print “This program provides concert seat assignments.” Ask the user their musical preference and take the following actions based on their potential input: Classical – proceed with the program (i.e. move the next requirement) – output a message that states “We do not recognize the musical genre.” Then end the program. Ask the user to enter the following data about themselves: • First Name • Last...
write a python program to do the following: ask a student for 3 coefficients a b...
write a python program to do the following: ask a student for 3 coefficients a b c using the high_school_quizz to display the solutions of a quadratic problem. after, ask the student if he would like another quadratic equation solved If the student answers anything but yes, the program terminates by displaying a good bye message if the student answers yes (any form of yes should be acceptable) , the student is asked for the coefficient again and the resulting...
Who knows to do this topic with python code? Write a program to perform the following...
Who knows to do this topic with python code? Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT