简体   繁体   中英

Higher/Lower game

import random

user_name = input('What is your name?')
print("Welcome to the higher/lower game,", user_name + "!")
lower = int(input("Enter the lower bound:"))
upper = int(input("Enter the upper bound:"))
correct_num = random.randint(lower, upper)

if lower > upper:
    lower = int(input("Enter the lower bound:"))
    upper = int(input("Enter the upper bound:"))
else:
    correct_num = 0

guess = True
while guess:
    print("Great, now guess a number between", str(lower), "and", str(upper), ":")
    user_guess = int(input('Enter guess:'))
    if user_guess < correct_num:
        print("Nope, too low!")
    elif user_guess > correct_num:
        print("Nope, too high")
    else:
        user_guess == correct_num
        print("You got it!")
        break

Here is what I have. I need help with having a random number chosen from the lower and upper bounds inputs.

You're assigning the upper and lower twice in your code. You can eliminate it and it should work fine.

lower = int(input("Enter the lower bound:"))
upper = int(input("Enter the upper bound:"))

if lower > upper:
    correct_num = random.randint(lower, upper)
else:
    correct_num = 0 

Also, you can simplify the code by removing guess = True and changing the loop to while True: since you never change the variable to break the loop and instead use break to stop the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM