In: Computer Science
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.
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