So I have a game where you have to guess a number the code picks at random (I was looking at project ideas), before taking a look at the answer I want to know if there is a more efficient to store the if's? I do think there might be a way to merge them into one single block and delete the others.
def guess(x): # this is a number generator
ran_num = random.randint(1, x)
guess = int(input('type number guess from 1 to {} '.format(x)))
attempt_num = 4
if attempt_num < 1:
print('game over')
exit()
elif guess < ran_num:
(print('your guess was a bit low'))
elif guess > ran_num:
print('your guess was all too high')
while guess != ran_num:
guess = int(input('oh no, your number is incorrect, please type that again, you gots {} attempts left '.format(attempt_num)))
attempt_num -= 1
if attempt_num < 1:
print('game over')
exit()
elif guess < ran_num:
(print('your guess was a bit low'))
elif guess > ran_num:
print('your guess was all too high')
if guess == ran_num:
print("success")
guess(5)
CodePudding user response:
Just ask for the guess inside the loop. You can use a for
loop to iterate 4 times.
def guess(x): # this is a number generator
ran_num = random.randint(1, x)
for attempt_num in range(4, 0, -1):
guess = int(input('type number guess from 1 to {}, you gots {} attempts left '.format(x, attempt_num)))
elif guess < ran_num:
(print('your guess was a bit low'))
elif guess > ran_num:
print('your guess was all too high')
else:
print("success")
break
else:
print('game over')
guess(5)
CodePudding user response:
You have some repeated code, so it would be a good idea to just put the if-else
blocks into a function, this will simplify your code.
I added a check_guess
function, and made ATTEMPT_NUM
global so we can reference it and update it in both functions.
import random
ATTEMPT_NUM = 4
def guess(x): # this is a number generator
global ATTEMPT_NUM
ran_num = random.randint(1, x)
guess = int(input('type number guess from 1 to {} '.format(x)))
check_guess(ran_num, guess)
while guess != ran_num:
guess = int(input('oh no, your number is incorrect, please type that again, you gots {} attempts left '.format(ATTEMPT_NUM)))
ATTEMPT_NUM -= 1
check_guess(ran_num, guess)
if guess == ran_num:
print("success")
def check_guess(ran_num, guess):
global ATTEMPT_NUM
if ATTEMPT_NUM < 1:
print('game over')
exit()
elif guess < ran_num:
(print('your guess was a bit low'))
elif guess > ran_num:
print('your guess was all too high')
guess(5)
CodePudding user response:
I made this attempt. x was the max random number you could get, right? Combine everything in an infinite loop that only breaks after you win or you put wrong all the numbers.
def guess(x):
attempt_num = 5
ran_num = random.randint(1, x)
while True:
guess = int(input('type number guess from 1 to {} '.format(x)))
if guess == ran_num:
print ("congrats")
break
elif guess < ran_num:
(print('your guess was a bit low'))
attempt_num = attempt_num -1
elif guess > ran_num:
print('your guess was all too high')
attempt_num = attempt_num -1
print (attempt_num)
if attempt_num == 0:
print ("game over")
break
It wouldn't hurt if you do a small check in the input to avoid the user putting anything else than a number, though.