Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.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_Range - Fatal编程技术网

Python限制在一个范围内

Python限制在一个范围内,python,range,Python,Range,我正在解决一个问题,它告诉我创建一个程序,根据拨号盘上的“点击”次数计算温度。温度从40开始,然后停止,再到90,一旦停止,温度会回到40,然后重新开始 clicks_str = input("By how many clicks has the dial been turned?") clicks_str = int(clicks_str) x = 40 x = int(x) for i in range(1): if clicks_str > 50: pr

我正在解决一个问题,它告诉我创建一个程序,根据拨号盘上的“点击”次数计算温度。温度从40开始,然后停止,再到90,一旦停止,温度会回到40,然后重新开始

clicks_str = input("By how many clicks has the dial been turned?")
clicks_str = int(clicks_str)

x = 40
x = int(x)

for i in range(1):
    if  clicks_str > 50:
        print("The temperature is",clicks_str -10)
    elif clicks_str < 0:
        print("The temperature is",clicks_str +90)
    else:
        print("The temperature is", x + clicks_str)
clicks\u str=input(“拨号盘转动了多少次点击?”)
点击次数=整数(点击次数)
x=40
x=int(x)
对于范围(1)中的i:
如果单击>50:
打印(“温度为”,单击\u str-10)
elif单击\u str<0:
打印(“温度为”,点击\u str+90)
其他:
打印(“温度为”,x+点击)

当我点击输入1000次时,温度自然会上升到990。我可以从代码中看出这一点,但如何使“温度”是一个介于40和90之间的数字。

如果将温度表示为一个介于0和50(90-40)之间的数字,则可以使用模运算,然后添加40以获得原始温度

clicks_str = input("By how many clicks has the dial been turned?")
clicks_str = int(clicks_str)

temp = (clicks_str % 51) + 40
print("The temperature is {}".format(temp))

您的代码可以是这样的,您不需要将数字转换为int,您可以在一行代码中输入int:

clicks_str = int(input("By how many clicks has the dial been turned?"))

x = 40

if  clicks_str > 50:
    print("The temperature is",clicks_str -10)
elif clicks_str < 0:
    print("The temperature is",clicks_str +90)
else:
    print("The temperature is", x + clicks_str)
clicks\u str=int(输入(“拨盘转动了多少次?”)
x=40
如果单击>50:
打印(“温度为”,单击\u str-10)
elif单击\u str<0:
打印(“温度为”,点击\u str+90)
其他:
打印(“温度为”,x+点击)

当您输入clicks\u str==1000或任何大于50的值时,您的输出是:
clicks\u str-10

问题似乎在于,当您不知道需要修改
clicks\u str
的次数,直到得到一个介于40和90之间的值时,您使用了范围函数。您还可以在每次修改
单击时打印“温度”,但它可能不是正确的温度(直到您获得介于0和50之间的
单击时)

解决此问题的更好方法是使用while循环:

clicks_str = int(input("By how many clicks has the dial been turned?"))
x = 40

while True:
    if  clicks_str > 50:
        clicks_str -= 50
    elif clicks_str < 0:
        clicks_str += 50
    else:
        print("The temperature is", x + clicks_str)
        break # breaks while loop

对于范围(1)中的i:
对您意味着什么?我相信你可以很容易地从你的代码中去掉它。因此while循环确保循环通过的数字是40-90??在这种情况下while循环将一直运行,直到遇到中断。正如您所看到的,只有在0clicks_str = int(input("By how many clicks has the dial been turned?")) x = 40 temp = (clicks_str % 50) + x print("The temperature is {}".format(temp))