0

Solving a shipping programming problem for school, just started with python 2.7.5, trying to make US or Canada a choice, as it stands I had to make a numerical choice to get this to work, I'm trying to get the choice for the prompt as US or Canada and not assign a number, do I have to declare something as a string? If I use Canada or US it gives me an error message about a global variable. rough draft with number choice:

def main ():
    user_ship_area = input('Are you shipping to the US or Canada? Type 1 for US, 2 for   Canada') 

    if user_ship_area != 2:
      print 'confirmed, we will ship to the United States '
    else:
      print "confirmed, we will ship to Canada" 

main() 

I get an Error message when I use Canada or US under if

user_ship_area = input('Are you shipping to the US or Canada?') 
if user_ship_area != Canada:
    print 'confirmed, we will ship to the United States '
else:
    print "confirmed, we will ship to Canada" 
JuanB457
  • 107
  • 2
  • 12
  • 1
    You should write 'Canada' as string literal, not a directive Canada – Denis Oct 10 '13 at 14:47
  • 1
    You should use `raw_input` rather than `input`. `raw_input` will take a string from the user: `input` will take a Python expression (like an integer 2) – David Robinson Oct 10 '13 at 14:48
  • http://stackoverflow.com/questions/7709022/is-it-ever-useful-to-use-pythons-input-over-raw-input – Josh Lee Oct 10 '13 at 14:50

3 Answers3

2

Use raw_input instead of input

def main ():
    user_ship_area = raw_input('Are you shipping to the US or Canada?') 

    if user_ship_area != 'Canada':
        print 'confirmed, we will ship to the United States '
    else:
        print "confirmed, we will ship to Canada" 

main() 
Alexander Zhukov
  • 4,357
  • 1
  • 20
  • 31
1

In your code, Canada will be parsed as variable, but it should be a string. Also, if you're using Python 2.x, then use raw_input instead of input, because the second one will evaluate your inputted string. So, your code should looks like:

user_ship_area = raw_input('Are you shipping to the US or Canada?') 
if user_ship_area != 'Canada':
    print 'confirmed, we will ship to the United States '
else:
    print "confirmed, we will ship to Canada" 
awesoon
  • 32,469
  • 11
  • 74
  • 99
0

You missed a ' mark on

user_ship_area = input('Are you shipping to the US or Canada?') #<--- here 
  #<--an indent here.  v      v  quotes to indicate string here  
  if user_ship_area != 'Canada':
    print 'You picked Canada!'
gregb212
  • 799
  • 6
  • 10