[英]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.您在代码中分配了两次 upper 和 lower 。 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.此外,您可以通过删除guess = True
并将循环更改为while True:
来简化代码,因为您永远不会更改变量来中断循环,而是使用break
来停止循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.