import random
# Define function - Number Generator
def num_gen():
num = random.randrange(1, 101)
return num
# Define function - Number Check
def num_check(x, y):
result = ''
if x > y:
result = 'high'
elif x < y:
result = 'low'
else:
result = 'correct'
return result
# Call - Number Generator
num = num_gen()
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
att = 1
# Process - Guess and Display Result
result = num_check(guess, num)
if result == 'high':
guess = int(input('Your guess was HIGH! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
elif result == 'low':
guess = int(input('Your guess was LOW! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
else:
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
CodePudding user response:
After getting input from console, you should create a loop which breaks when guessed number equal to input. For example:
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
att = 1
# Process - Guess and Display Result
result = num_check(guess, num)
while result != guess:
if result == 'high':
guess = int(input('Your guess was HIGH! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
elif result == 'low':
guess = int(input('Your guess was LOW! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
else:
break
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
After revision, code seems like:
import random
# Define function - Number Generator
def num_gen():
num = random.randrange(1, 101)
return num
# Define function - Number Check
def num_check(x, y):
result = ''
if x > y:
result = 'high'
elif x < y:
result = 'low'
else:
result = 'correct'
return result
# Call - Number Generator
num = num_gen()
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
att = 1
# Process - Guess and Display Result
result = num_check(guess, num)
while result != guess:
if result == 'high':
guess = int(input('Your guess was HIGH! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
elif result == 'low':
guess = int(input('Your guess was LOW! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
else:
break
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
CodePudding user response:
It's because python programs runs from top to bottom, and when it reaches the bottom it's done and stops running. To stop this order we have multiple different ways:
We can call a function, as you do in you're program.
Selection Control Structures: As if statements
Iteration Control Structures: As loops
The last one is the one you need in this solution. Wrap code in a while loop, with the correct condition, in you're example, this would be
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
att = 1
# Process - Guess and Display Result
result = num_check(guess, num)
if result == 'high':
guess = int(input('Your guess was HIGH! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
elif result == 'low':
guess = int(input('Your guess was LOW! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
else:
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
The best way woul be to use a will loop as follow:
while guess != num:
CodePudding user response:
You should use while True:, because your guessing code runs only once without it
import random
# Define function - Number Generator
def num_gen():
num = random.randrange(1, 10)
return num
# Define function - Number Check
def num_check(x, y):
result = ''
if x > y:
result = 'high'
elif x < y:
result = 'low'
else:
result = 'correct'
return result
# Call - Number Generator
att = 1
num = num_gen()
while True:
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
# Process - Guess and Display Result
result = num_check(guess, num)
if result == 'high':
print('Your guess was HIGH! Please guess another number between 1 and 100: ')
att = 1
elif result == 'low':
print('Your guess was LOW! Please guess another number between 1 and 100: ')
att = 1
else:
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
break
CodePudding user response:
how can you expect it to run more than 1 time if you have not used any loop in the program. For example in this case you should use a while loop, then only it will prompt the user again and again and will check for the conditions, set a game variable to be True. When the number is matched then simply set the game variable to be False and it will end the loop.
import random
# Set the game variable initially to be True
game = True
# Define function - Number Generator
def num_gen():
num = random.randrange(1, 101)
return num
# Define function - Number Check
def num_check(x, y):
result = ''
if x > y:
result = 'high'
elif x < y:
result = 'low'
else:
result = 'correct'
return result
# Call - Number Generator
num = num_gen()
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
att = 1
# Process - Guess and Display Result
result = num_check(guess, num)
while game:
if result == 'high':
guess = int(input('Your guess was HIGH! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
elif result == 'low':
guess = int(input('Your guess was LOW! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
else:
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
game = False
