Jump to content

Python Question

X1XNobleX1X
Hello forums

I'm having trouble with Python:

Here is what I have:

 


temp = int(input("Temperature: "))

while temp != 42:

  if temp < 42:

     print("Too cold!")

  if temp > 42: 

    print("Too hot!")

  if temp == 42:

    print("Just right")

    


I'm very frustrated it's suppose to this this:

 

Here is the example.

 


Temperature: 12

Too cold!

Temperature: 36

Too cold!

Temperature: 39

Too cold!

Temperature: 46

Too hot!

Temperature: 51

Too hot!

Temperature: 43

Too hot!

Temperature: 42

Just right!

 

But it doesn't !

 

Thanks for all the help!

 


 


|CPU: Intel 5960X|MOBO:Rampage V Extreme|GPU:EVGA 980Ti SC 2 - Way SLI|RAM:G-Skill 32GB|CASE:900D|PSU:CorsairAX1200i|DISPLAY :Dell U2412M X3|SSD Intel 750 400GB, 2X Samsung 850 Pro|

Peripherals : | MOUSE : Logitech G602 | KEYBOARD: K70 RGB (Cherry MX Brown) | NAS: Synology DS1515+  - WD RED 3TB X 5|ROUTER: AC68U

Sound : | HEADPHONES: Sennheiser HD800 SPEAKERS: B&W CM9 (Front floorstanding) ,  B&W CM Center 2 (Centre) | AV RECEIVER : Denon 3806 | MY X99 BUILD LOG!

 

Link to comment
Share on other sites

Link to post
Share on other sites

change the first line to 

while True:     temp=raw_input ("Temperature: ") 

then everything else

you know what? I'll just change it to how I see it works:

while True:  temp=raw_input ("Temperature: ")  if temp < 42:     print("Too cold!")  elif temp > 42:     print("Too hot!")  elif temp == 42:    print("Just right")  else:    print("probably dead...")

Home is where the heart my desktop is.

Link to comment
Share on other sites

Link to post
Share on other sites

change the first line to 

while True:     temp=raw_input ("Temperature: ") 

then everything else

If he wants the loop to exit when 42 is entered, then there's no need to make it an infinite loop.

Link to comment
Share on other sites

Link to post
Share on other sites

          

 

If he wants the loop to exit when 42 is entered, then there's no need to make it an infinite loop.

Tha'ts what I want it to do,

What do I need to change?

|CPU: Intel 5960X|MOBO:Rampage V Extreme|GPU:EVGA 980Ti SC 2 - Way SLI|RAM:G-Skill 32GB|CASE:900D|PSU:CorsairAX1200i|DISPLAY :Dell U2412M X3|SSD Intel 750 400GB, 2X Samsung 850 Pro|

Peripherals : | MOUSE : Logitech G602 | KEYBOARD: K70 RGB (Cherry MX Brown) | NAS: Synology DS1515+  - WD RED 3TB X 5|ROUTER: AC68U

Sound : | HEADPHONES: Sennheiser HD800 SPEAKERS: B&W CM9 (Front floorstanding) ,  B&W CM Center 2 (Centre) | AV RECEIVER : Denon 3806 | MY X99 BUILD LOG!

 

Link to comment
Share on other sites

Link to post
Share on other sites

 

          

 

Tha'ts what I want it to do,

What do I need to change?

 

welll....sorry but I don't know how to break a loop upon a specific input...

But what I proposed doesn't work anyway, I fixed it a little. Turns out the use of greater than/less than signs specifically require you convert the input to a floating number. my bad, I didn't notice the first line of your code...

Basically everything else is working except the loop parameters. I'm sorry, I'm not helping much...

temp = int(input("Temperature: "))if temp ==42:    print ("Just right")if temp < 42:    print ("Too cold")if temp > 42:    print ("Too hot")    

Home is where the heart my desktop is.

Link to comment
Share on other sites

Link to post
Share on other sites

If he wants the loop to exit when 42 is entered, then there's no need to make it an infinite loop.

An alternate approach is to use an infinite loop, but using a break; statement for the condition if temp==42, after printing out "Just right". 

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

This is the way I would do it.

 

while (true) :
  temp = int(input("Temperature: "))
  if temp < 42:
     print("Too cold!")
  elif temp > 42:
    print("Too hot!")
  elif temp == 42:
    print("Just right")
    break

 

 

 

------------------------------

But, another way of doing it would be this:

 

temp = int(input("Temperature: "))   Get the users input
while (temp != 42 ):                         Make sure it is not 42
  if temp < 42:
     print("Too cold!")
  elif temp > 42:
    print("Too hot!")
  temp = int(input("Temperature: "))
print ("Just right")                             When it is 42, we just print "just right"
 

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, I do agree, I have tested for truth. Which is wrong.

 

It should actually be

 

while (true) :
  temp = int(input("Temperature: "))
  if temp < 42:
     print("Too cold!")
  elif temp > 42:
    print("Too hot!")
  else:
    print("Just right")
    break

 

Sorry about that mistake.

Link to comment
Share on other sites

Link to post
Share on other sites

Personally I would go with

 

while (lambda x: print("Too cold!") or 1 if x < 42 else print("Too hot!") or 1 if x > 42 else print("Just right"))(int(input("Temperature: "))): pass

 

 

Don't do that.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

An alternate approach is to use an infinite loop, but using a break; statement for the condition if temp==42, after printing out "Just right". 

True, but why modify a line that doesn't need changing and add a new line when you don't have to :P

 

Although I suppose then you'd have to initialize temp anyway for it to enter the loop. Eh, whatever, it's a simple problem so it doesn't really matter.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×