我如何限制用户只让他输入大于 minimum_range 的数字?
how can i limit the user by letting him input a number only if higher than minimum_range?
import random
def input_range():
minimum_range = 5
users_range = input("set the maximum value for the range, minimum " + minimum_range + ":")
if int(users_range) > int(minimum_range):
print("The maximum range you selected is:", users_range)
else:
print("Out of range, try again")
random_number = random.randint(int(minimum_range), int(users_range))
print(random_number)
def name_user_request():
users_name = input("what's yor name? ")
print("Hi " + users_name + " nice to meet you")
input_range()
name_user_request()
如果我输入的数字小于 minimum_range(在本例中为 5),我发布的这段代码会出现错误,任何帮助都将非常有用。
谢谢!
您可以使用 while
循环一遍又一遍地请求有效输入(我将其他行更改为使用 f-strings):
import random
def input_range():
minimum_range = 5
prompt = f"Set the maximum value for the range, minimum {minimum_range}: "
while (users_range := int(input(prompt))) < minimum_range:
print("Out of range, try again")
print(f"The maximum range you selected is: {users_range}")
random_number = random.randint(minimum_range, users_range)
print(random_number)
def name_user_request():
users_name = input("what's yor name? ")
print(f"Hi {users_name}, nice to meet you!")
input_range()
name_user_request()
请注意,它使用“海象”运算符 :=
(在 python 3.8 中引入)。如果没有海象运算符,您可以将 while
部分替换为
users_range = int(input(prompt))
while users_range < minimum_range:
print("Out of range, try again")
users_range = int(input(prompt))
尝试递归!!
import random
def input_range():
minimum_range = 5
users_range = input("set the maximum value for the range, minimum " + str(minimum_range) + ":")
if int(users_range) > int(minimum_range):
print("The maximum range you selected is:", users_range)
random_number = random.randint(int(minimum_range), int(users_range))
print(random_number)
return
else:
print("Out of range, try again")
input_range()
return
def name_user_request():
users_name = input("what's yor name? ")
print("Hi " + users_name + " nice to meet you")
input_range()
name_user_request()
import random
def input_range():
minimum_range = 5
users_range = input("set the maximum value for the range, minimum " + minimum_range + ":")
if int(users_range) > int(minimum_range):
print("The maximum range you selected is:", users_range)
else:
print("Out of range, try again")
random_number = random.randint(int(minimum_range), int(users_range))
print(random_number)
def name_user_request():
users_name = input("what's yor name? ")
print("Hi " + users_name + " nice to meet you")
input_range()
name_user_request()
如果我输入的数字小于 minimum_range(在本例中为 5),我发布的这段代码会出现错误,任何帮助都将非常有用。 谢谢!
您可以使用 while
循环一遍又一遍地请求有效输入(我将其他行更改为使用 f-strings):
import random
def input_range():
minimum_range = 5
prompt = f"Set the maximum value for the range, minimum {minimum_range}: "
while (users_range := int(input(prompt))) < minimum_range:
print("Out of range, try again")
print(f"The maximum range you selected is: {users_range}")
random_number = random.randint(minimum_range, users_range)
print(random_number)
def name_user_request():
users_name = input("what's yor name? ")
print(f"Hi {users_name}, nice to meet you!")
input_range()
name_user_request()
请注意,它使用“海象”运算符 :=
(在 python 3.8 中引入)。如果没有海象运算符,您可以将 while
部分替换为
users_range = int(input(prompt))
while users_range < minimum_range:
print("Out of range, try again")
users_range = int(input(prompt))
尝试递归!!
import random
def input_range():
minimum_range = 5
users_range = input("set the maximum value for the range, minimum " + str(minimum_range) + ":")
if int(users_range) > int(minimum_range):
print("The maximum range you selected is:", users_range)
random_number = random.randint(int(minimum_range), int(users_range))
print(random_number)
return
else:
print("Out of range, try again")
input_range()
return
def name_user_request():
users_name = input("what's yor name? ")
print("Hi " + users_name + " nice to meet you")
input_range()
name_user_request()