Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的基本循环_Python - Fatal编程技术网

Python中的基本循环

Python中的基本循环,python,Python,新来的,所以请容忍我。我试图运行一个循环,要求用户输入一个介于1和100之间的数字。我想知道,如果他们输入的数字超过100,它会再次询问。我可以这样做,但我不知道我是否使用了正确的循环。而且,每当我进入1和100之间时,循环就会继续 代码如下: user_input = int(input("Enter a number between 1 and 100: ")) if user_input >= 1 and user_input <= 100: print("NICE!"

新来的,所以请容忍我。我试图运行一个循环,要求用户输入一个介于1和100之间的数字。我想知道,如果他们输入的数字超过100,它会再次询问。我可以这样做,但我不知道我是否使用了正确的循环。而且,每当我进入1和100之间时,循环就会继续

代码如下:

user_input = int(input("Enter a number between 1 and 100: "))
if user_input >= 1 and user_input <= 100:
    print("NICE!")
else: 
    while user_input > 100:
        try_again = int(input("try again "))
        if try_again >= 1 and try_again <= 100:
            print("There you go!")
user\u input=int(输入(“输入1到100之间的数字”))
如果用户输入>=1且用户输入100:
try_reach=int(输入(“重试”))

如果在代码运行时重试>=1并重试,它将继续请求输入,即使给定的输入小于100。解决此问题的一种方法是:

try_again = 1000
user_input = int(input("Enter a number between 1 and 100: "))
if user_input >= 1 and user_input <= 100:
    print("NICE!")
elif user_input > 100:
        while try_again > 100:
        try_again = int(input("try again "))
        if try_again >= 1 and try_again <= 100:
            print("There you go!")
再试一次=1000
用户输入=int(输入(“输入一个介于1和100之间的数字:”)
如果用户输入>=1且用户输入100:
当重试>100时:
try_reach=int(输入(“重试”))

如果try\u reach>=1和try\u reach我认为最清晰的方法是从一个循环开始,当你最终得到正确的答案时,你会打破这个循环。一定要处理像“fubar”这样不是整数的错误输入

while True:
    try:
        user_input = int(input("Enter a number between 1 and 100: "))
        if user_input >= 1 and user_input <= 100:
            print("NICE!")
            break
        print("Not between 1 and 100, try again")
    except ValueError:
        print("Not a number, try again")

range
将计算结果,而不实际生成所有数字。

下面是一个程序示例,该程序可获取您正在查找的输出:

attempts = 0

while True:
     user_input = int(input("Enter a number between 1 and 100: "))

     if user_input > 100 or user_input < 1:
          print('Please try again')
          attempts += 1
          continue
     elif attempts >= 1 and user_input <= 100 and user_input >= 1:
          print('There you go!')
          break
     else:
          print('Nice!')
          break
尝试次数=0
尽管如此:
用户输入=int(输入(“输入一个介于1和100之间的数字:”)
如果用户输入>100或用户输入<1:
打印('请重试')
尝试次数+=1
持续
elif尝试次数>=1且用户输入=1:
打印('给你!')
打破
其他:
打印('Nice!')
打破

首先,在循环中输入用户提示,以便在用户首次输入1到100之间的数字失败时,可以向用户发出相同的提示。如果用户输入大于100或小于1,我们将告诉用户重试,我们将在尝试中添加1,并添加一个continue语句,该语句将在while循环的顶部再次启动代码。接下来我们添加一个elif语句。如果他们已经尝试提示但失败(尝试次数>=1),并且如果新输入小于或等于100,并且用户输入也大于或等于1,则用户将收到您分配给他们的“There you go”(开始)消息。然后我们将使用break语句中断循环,以避免无限循环。最后,我们添加一个else语句。如果用户在第一次尝试时满足先前的条件,我们将打印“Nice”并简单地中断循环。

哦,好的,谢谢。是的,我在网上看过,但我只是想从我的代码中得到反馈。谢谢哦,还有,假设用户输入的数字一直高于100。。。什么是让他们“再试一次”直到得到它的好方法?不要再次运行代码,我看不到
elif
如何再次提示用户。是的,我想你有不同的观点,我会修改我的答案。谢谢!特德莱尼。。。那帮了大忙!
attempts = 0

while True:
     user_input = int(input("Enter a number between 1 and 100: "))

     if user_input > 100 or user_input < 1:
          print('Please try again')
          attempts += 1
          continue
     elif attempts >= 1 and user_input <= 100 and user_input >= 1:
          print('There you go!')
          break
     else:
          print('Nice!')
          break