patternpythonMinor
Beginning guessing game in Python
Viewed 0 times
beginninggamepythonguessing
Problem
I've just finished work on my guessing game. I'm still fairly new to Python, so I'm looking for feedback and criticism on the code.
```
def game():
import random #imports the module 'random'
yes={"Yes","Y","yes","y"} #set 'yes' to these 4 strings
no={"No","N","no","n"} #set 'no' to these 4 strings
name=input("What is your name? \n") #asks for a name
secret=int(random.random()*10)+1 #randomly generate a number
trynum=0 #sets the number of tries to 0
print ("---------------------------------------------------------------------\n"
"---- Welcome,",name+"!\n"
"---- I am thinking of a number between 1 and 10.\n"
"---- Lets see how many times it will take you to guess the number.\n"
"---------------------------------------------------------------------\n")
while True: #starts a loop
trynum=trynum+1 #sets number of tries to itself + 1
guess=int(input("What is guess #"+str(trynum)+"? \n")) #asks for a guess
if guesssecret: #if guess is too high/greater than(>)
print ("Oops!! Too high, better luck next try!")
else:
if trynum==1: #if number of tries is only 1
print ("-------------------------------------------------\n"
"----",name+"! You got it in",trynum,"guess!\n"
"-------------------------------------------------\n")
else: #if number of tries is anything else
print ("-------------------------------------------------\n"
"----",name+"! You got it in",trynum,"guesses!\n"
"-------------------------------------------------\n")
if trynum<3: #if guessed in less than 3 tries
print ("Bravo!")
elif trynum<5: #if guessed in less than 5 tries
print ("Hmmmmpf... Better try again")
elif trynum<7: #if guessed in less than 7 tries
```
def game():
import random #imports the module 'random'
yes={"Yes","Y","yes","y"} #set 'yes' to these 4 strings
no={"No","N","no","n"} #set 'no' to these 4 strings
name=input("What is your name? \n") #asks for a name
secret=int(random.random()*10)+1 #randomly generate a number
trynum=0 #sets the number of tries to 0
print ("---------------------------------------------------------------------\n"
"---- Welcome,",name+"!\n"
"---- I am thinking of a number between 1 and 10.\n"
"---- Lets see how many times it will take you to guess the number.\n"
"---------------------------------------------------------------------\n")
while True: #starts a loop
trynum=trynum+1 #sets number of tries to itself + 1
guess=int(input("What is guess #"+str(trynum)+"? \n")) #asks for a guess
if guesssecret: #if guess is too high/greater than(>)
print ("Oops!! Too high, better luck next try!")
else:
if trynum==1: #if number of tries is only 1
print ("-------------------------------------------------\n"
"----",name+"! You got it in",trynum,"guess!\n"
"-------------------------------------------------\n")
else: #if number of tries is anything else
print ("-------------------------------------------------\n"
"----",name+"! You got it in",trynum,"guesses!\n"
"-------------------------------------------------\n")
if trynum<3: #if guessed in less than 3 tries
print ("Bravo!")
elif trynum<5: #if guessed in less than 5 tries
print ("Hmmmmpf... Better try again")
elif trynum<7: #if guessed in less than 7 tries
Solution
Good job for a first experience in Python! You can improve your code a little by looking at these recommendations:
-
Personally, I wouldn't use lists with "yes" and "no" values, try something like
-
Last but not least, you should check if the number entered is an integer. If it is not, ask the user to enter a valid integer until they do so.
- To choose a random integer, you could simply use
random.randrange(0,11)
- You should probably not import at the function level either, try writing your import statements at the top of your file.
- The
game()function is called recursively; you shouldn't do that. You could have an infinite loop that firesgame()back when exited.
-
Personally, I wouldn't use lists with "yes" and "no" values, try something like
if "y" in choice.lower():
return True
else:
return False-
Last but not least, you should check if the number entered is an integer. If it is not, ask the user to enter a valid integer until they do so.
Code Snippets
if "y" in choice.lower():
return True
else:
return FalseContext
StackExchange Code Review Q#129051, answer score: 4
Revisions (0)
No revisions yet.