Reputation: 1
i just dont undersand why it dosent work and what i need to do to fix it. the goal is to make a counting game so i any tips one a restart button would be great aswell. there is no error i just dosent get out of the first loop.
import random
secrectnumber = random.randint(1,100)
nummberguess = 5
print("guess a Number between 1 and 100")
number = False
wrongguess = True
while wrongguess and nummberguess>0:
guess=input()
print( "writte a number")
if guess.isdigit():
guess=int(guess)
number = True
wrongguess = False
else:
print("invalid input")
while number:
nummberguess=nummberguess-1
if guess== secrectnumber:
print("you did it")
break
elif secrectnumber>guess:
print("the number is higeher")
wrongguess = True
elif secrectnumber<guess:
print("the number is lower")
wrongguess = True
Upvotes: 0
Views: 34
Reputation: 1365
It can be done very simply as :
import random
secrectnumber = random.randint(1, 100)
nummberguess = 5
print("guess a Number between 1 and 100")
for i in range(nummberguess):
guess = input()
if guess.isdigit():
guess = int(guess)
if guess == secrectnumber:
print("you did it")
break
elif secrectnumber > guess:
print("the number is higeher.", end="")
if i == nummberguess -1:
print(" Exhausted !!!")
else:
print(" Try again !!!")
elif secrectnumber < guess:
print("the number is lower.", end="")
if i == nummberguess -1:
print("Exhausted !!!")
else:
print("Try again !!!")
Upvotes: 0
Reputation: 141
There's 2 things wrong with your code based on what you wrote:
while wrongguess
is always true
and nummberguess>0
is also always true because you're not decrementing/incrementing it. What is that supposed to do? Terminate if number_of_guess ≥ 5? You need to add a counter to actually terminate the first loop after max number of guesses is reached or whatever the desired output may be.
guess
if the number is not the intended secret number.
import random
secrectnumber = random.randint(1,100)
nummberguess = 5
print("guess a Number between 1 and 100")
number = False
wrongguess = True
while wrongguess and nummberguess>0:
guess=input()
print( "writte a number")
if guess.isdigit():
# this won't work for input = 31, you need to iterate the string instead and check if all characters are digits, something like this:
# sum([x.isdigit() for x in str(guess)]) == len(guess) <-- Number, this won't work for floating point numbers because '3', '.', '1', '4' (3, 1, 4 is digit, but '.' isn't). Just something to think about
guess=int(guess)
number = True
wrongguess = False
else:
print("invalid input")
nummberguess -= 1
if guess== secrectnumber:
print("you did it")
break
elif secrectnumber>guess:
print("the number is higeher")
wrongguess = True
elif secrectnumber<guess:
print("the number is lower")
wrongguess = True
Upvotes: 1