Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python confining within a range

Tags:

python

range

I am working on a problem that tells me to create a program that calculates temperature depending on how many "clicks" on a dial. The temperature starts at 40 and stops and 90 and once it stops it goes back to 40 and starts over.

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)

When I put input 1000 clicks, the temperature naturally goes to 990. I can see that from the code, but how would I make it so the "temperature" is a number in between 40 and 90.

like image 586
Cato Savanah Avatar asked Jul 29 '26 02:07

Cato Savanah


1 Answers

If you represent the temperature as a number between 0 and 50 (90-40), you could use the modulo operation and then add 40 to get the original temperature.

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))
like image 187
fedterzi Avatar answered Jul 31 '26 00:07

fedterzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!