如何获取代码让用户在 python 中的不同范围的随机数之间进行选择?
How to get the code to make the user pick between different ranges of random numbers in python?
我想编写一个代码,从 3 种不同的选择中生成随机数,用户可以从中选择,但代码无法识别我用来表示这些选择的变量。这是我到目前为止所得到的:
import random
choose = int(input("1, 2 or 3? "))
if choose == 1:
num == random.randint(1, 10)
elif choose == 2:
num == random.randint(10, 50)
elif choose == 3:
num == random.randint(1, 100)
else:
print("Invalid input")
print(num)
有人可以帮忙吗?
有些混乱。 ==
用于 比较 就像 is a equal to b?
和 =
用于赋值。所以,只需替换为 num = random.randint(10, 50)
.
此外,您应该考虑将 print(num)
移到 if...elif...else
语句中,因为它可能会在提供无效输入时引发错误。
import random
choose = int(input("1, 2 or 3? "))
if choose == 1:
num = random.randint(1, 10)
print("Your Number: "num)
elif choose == 2:
num = random.randint(10, 50)
print("Your Number: "num)
elif choose == 3:
num =random.randint(1, 100)
print("Your Number: "num)
else:
print("Invalid input")
我想编写一个代码,从 3 种不同的选择中生成随机数,用户可以从中选择,但代码无法识别我用来表示这些选择的变量。这是我到目前为止所得到的:
import random
choose = int(input("1, 2 or 3? "))
if choose == 1:
num == random.randint(1, 10)
elif choose == 2:
num == random.randint(10, 50)
elif choose == 3:
num == random.randint(1, 100)
else:
print("Invalid input")
print(num)
有人可以帮忙吗?
有些混乱。 ==
用于 比较 就像 is a equal to b?
和 =
用于赋值。所以,只需替换为 num = random.randint(10, 50)
.
此外,您应该考虑将 print(num)
移到 if...elif...else
语句中,因为它可能会在提供无效输入时引发错误。
import random
choose = int(input("1, 2 or 3? "))
if choose == 1:
num = random.randint(1, 10)
print("Your Number: "num)
elif choose == 2:
num = random.randint(10, 50)
print("Your Number: "num)
elif choose == 3:
num =random.randint(1, 100)
print("Your Number: "num)
else:
print("Invalid input")