r/pythonhelp • u/gvleum- • Jan 25 '24
I am beginner I need guidance with a number game
I want to add a total of 10 tries and add I want to add how many tries it took the user to guess the number i don't know how please help me I'm trying again and again but keep failing please help with non complex codes
print("This game will give you 10 chances to guess a random number")
import random
secretnumber = random.randint(1,15)
while True:
guessnumber=int(input('enter your guess'))
if guessnumber> secretnumber:
print('The number is Too high, Try Again!!!')
elif guessnumber< secretnumber:
print('The number is Too Low, Try Again!!!')
elif guessnumber == secretnumber:
print('You have guessed is correct number...Congrulations!!!')
print('Thanks For Playing')
break
1
Upvotes
•
u/AutoModerator Jan 25 '24
To give us the best chance to help you, please include any relevant code.
Note. Do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Repl.it, GitHub or PasteBin.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/CraigAT Jan 25 '24
Very difficult to read without (Reddit) formatting but:
Import shoud be at the top.
If you want to allow 10 guesses you should change your loop to something like "for guess in range(10):" OR set guesses to 0 and then use "while guesses < 10:" and increment guesses with each loop. You can then use "break" whenever you find the correct answer.
1
u/streamer3222 Jan 26 '24
#1. Your program is correct and I hope you tested that it works. But one thing. Instead of saying
elif guessnumber == secretnumber:you could simply sayelse:because if it is not greater and it is not smaller, then it must be equal. But it is not wrong and it's good to always be explicit.#2. Between
secretnumber = random.randint(1,15)andwhile True:writenumberOfTries = 0. This states the user has not tried anything yet. Just afterguessnumber=int(input('enter your guess')), writenumberOfTries += 1. This adds 1 tonumberOfTriessignalling the user has tried one time.Now, instead of saying
while True:, you can saywhile numberOfTries < 10:.#3. If you wish to output the number of tries, look for the line
print('You have guessed is correct number...Congrulations!!!'); directly after it, addprint('The number of tries is: '), then on the next line addprint(numberOfTries).Other than that, good attempt and keep learning!