0

Python newb here. I'm looking to write an if statement that changes an integer value based upon an input and then loops the code. Unfortunately, I have two problems:

  1. When the brightness printers after accepting an input, the value that prints is 100 regardless of the input (-10, +10, set to 0, etc)
  2. The else statement prints regardless if the user enters a value that matches an if statement.

What am I doing wrong here?

# -*- coding: utf-8 -*-

var = 1
brightness = 100

while var == 1 :  # This constructs an infinite loop

  print 'Brightness is ', brightness

  test1 = raw_input('up, down, on or off? ')

  if test1 == 'up':
     brightness = brightness + 10
     print brightness
  if test1 == 'down':
     brightness = brightness - 10
     print brightness
  if test1 == 'on':
     brightness = 100
     print brightness
  if test1 == 'off':
     brightness = 0
     print brightness
  else:
     print 'Try again'

print "Good bye!"
8
  • Use else ifs. It's falling through Commented Sep 18, 2016 at 18:01
  • 1
    It is working fine in python 2.7.10 although i suggest that use if..elif..else instead of so many if Commented Sep 18, 2016 at 18:01
  • The code runs but I have issues with the #1 and #2 I had mentioned above. Commented Sep 18, 2016 at 18:04
  • the else only applies to the last if statement. You have to use elif statements in the middle if you want them all to be evaluated as a single block. Commented Sep 18, 2016 at 18:06
  • In my machine it prints appropriate value. Commented Sep 18, 2016 at 18:07

2 Answers 2

1
var = 1
brightness = 100

while var == 1 :  # This constructs an infinite loop

print 'Brightness is ', brightness

test1 = raw_input('up, down, on or off? ')

if test1 == 'up':
    brightness = brightness + 10
    print brightness
elif test1 == 'down':
    brightness = brightness - 10
    print brightness
elif test1 == 'on':
    brightness = 100
    print brightness
elif test1 == 'off':
    brightness = 0
    print brightness
else:
    print 'Try again'

print "Good bye!"
Sign up to request clarification or add additional context in comments.

2 Comments

The elif change fixed it on my end. Thank you!
"The else statement prints regardless if the user enters a value that matches an if statement." This is the role of the else statement. If the if statements don't match with any of the inputs, it will always go to the else. However, with if elif else, it shouldn't go to else if an if condition is reached.
0

Andrew L. and Kalpesh Dusane answered this, thank you!!!

I needed to exchange my if for elif (else if).

# -*- coding: utf-8 -*-

var = 1
brightness = 100

while var == 1 :  # This constructs an infinite loop

  print 'Brightness is ', brightness

  test1 = raw_input('up, down, on or off? ')

  if test1 == 'up':
     brightness = brightness + 10
     print brightness
  elif test1 == 'down':
     brightness = brightness - 10
     print brightness
  elif test1 == 'on':
     brightness = 100
     print brightness
  elif test1 == 'off':
     brightness = 0
     print brightness
  else:
     print 'Try again'

print "Good bye!"

The code works great, thanks!

1 Comment

if you don't modify var in any way, you can use while True for the same effect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.