Chase Westlye Chase Westlye - 2 months ago 7
Python Question

Changing integer values from an if statement not working? (Python)

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!"

Answer
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!"