i am trying to teach myself python using this book called, core python programing by wesley chung. one of the exersises in this book says make a text based menu application that uses more than one of the programs you made already.When i made the application one of my code blocks does not work for some reason and the other one with similar code does. please help me fix the problem and explain what i am doing wrong.

#declaring var that controls program
a=0
#menu funtion
def menu():
   print "\nmenu"
   print "\n1=type numbers"
   print "\n2=add 2 numbers"
   print "\n3=quit"
   return()
#loop that should repeat menu until a number that isnt 1 or 2 is pressed
while a==0:
    menu()
    a=int(raw_input("choose a menu number"))
# if 1 is pressed then this program prompts a
#a user for a number between 1 and 100
    if a==1:
       x=0
       number=int(raw_input("type a number between 1 and 100 "))
       while x==0:
          if number<100 and number>0:
             print "good job"
             x+=1
# this should restart the loop but it doesnt
          elif x==1:
             a=0
#this should ask for another number if the
#number pressed isnt between 1 and 100
#but it just leaves a blank space
          elif number>100 and number<0:
             number=int(raw_input("again"))
#from what i know this code works
    if a==2:
       x=0
       number=int(raw_input("what do u wanna add"))
       while x==0:
          number2=int(raw_input("number 2 please"))
          number3=number+number2
          print number3
#This does restart the loop
          x+=1
          if x==1:
             a=0

Recommended Answers

All 6 Replies

Here you go jascase i fixed the problem, i'm not good at explaining things but in the line of the code that didn't work where you asked for input for the 2 numbers.

number=int(raw_input("what do u wanna add"))
number2=int(raw_input("number 2 please"))

you just needed to add this to the end:

.strip())

and it will add the 2 numbers you put in, and i changed the if to elif. Heres the code:

#My changes here:
    elif a==2:
       x=0
       number = int(raw_input("what do u wanna add").strip())
       while x==0:
          number2 = int(raw_input("number 2 please").strip())
          number3 = number+number2
          print number3

thank you, i have fixed most of my code now but i have 2 questions.

i fixed

x+=1
          elif x==1:
             a=0

by changeing the elif an if. so my first question is why did it need to be an if?

my second question is when i type a number greater than 100 or less than 0 in my

if a==1:
       x=0
       number=int(raw_input("type a number between 1 and 100 "))
       while x==0:
          if number<100 and number>0:
             print "good job"
             x+=1

it just leaves a blank and i need to restart the shell.

this is the code thats suposed to tell the user to type another number if its <100 or >0

elif number>100 and number<0:
             number=int(raw_input("again"))

for your first question why elif? elif is just a shortened way of saying else if when the statement fails to be true, elif will do what is under IF the conditions are met. And for your second question i fixed it with else: after the first part asking for input here it is:

#My changes here
    if a==1:
       x=0
       number=int(raw_input("type a number between 1 and 100 "))
       while x==0:
        if number<100 and number>0:
             print "good job"
        else:
            print "Wrong number: %s" % number
            number = int(raw_input("try again: ").strip()) 
            print "good job"
            x+=1

thank you so much, your fix worked perfectly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.