-2

The goal is for the computer to guess a number given an upper and lower bound, the computer should take no as an input and run the program again until a yes is given.

This is the code I am using right now, but the loop only ever runs once dues to where the return function is. If I take that out and jump directly to print it runs continuously.

import random

num1 = int(input("enter your minumum value: "))
num2 = int(input("enter your maximum value: ")) 
def number_choice(num1,num2):
    guess = "no"
    while guess == "no": 
        return random.choice(range(num1,num2))
print (number_choice(num1,num2))
guess = input("is that your number, enter yes or no: ")
  • 1
    `return` always ends the execution of the method in which it resides. This is clearly spelled out in the [relevant documentation](https://docs.python.org/3/reference/simple_stmts.html#the-return-statement): “*`return` leaves the current function call with the expression list (or None) as return value*”. – esqew Aug 03 '21 at 14:44

1 Answers1

2

Try this:

import random

num1 = int(input("enter your minimum value: "))
num2 = int(input("enter your maximum value: "))
def number_choice(num1,num2):
        print(random.choice(range(num1,num2)))
guess='no'
while guess.lower()=='no':
    number_choice(num1,num2)
    guess = input("Is that your number, enter yes or no: ")
print('Cheers!')

or you can use this too:

import random

num1 = int(input("enter your minimum value: "))
num2 = int(input("enter your maximum value: ")) 
def number_choice(num1,num2):
    print(rnd.choice(range(num1,num2)))
    guess=input('Is this your number? Type yes or no ')
    if guess=='no':
        number_choice(num1,num2)
    else:
        print('cheers!')

number_choice(num1,num2)

Hope it helps!