Jump to content

Python Nub


The Sorrow

Recommended Posts

I wanted to know how to edit this code to make it loop back to the beginning. Its just a number comparison.

num1 = 0
num2 = 0

print "Hello, im a number comparison program "

num1 = input("Enter a number ")
num2 = input("Enter another number ")

if (num1 > num2):
    print "Your first number is greater"

if (num2 > num1):
    print "Your second number is greater"

if (num1 == num2):
    print "Your numbers are equal"

here is how i want to finish it in pseudo-code:

 "Enter another? (Yes or No) > "

if yes loop to beginning
if no print "Goodbye :)"

any help?

Edited by The Sorrow
Link to comment
Share on other sites

Yea i figured it out.

num1 = 0
num2 = 0
menuOption = ""

print "Hello, I'm a number comparison program "
while (menuOption != "n"):
     num1 = input("Enter a number> ")
     num2 = input("Enter another number> ")

     if (num1 > num2):
          print "Your first number is greater"

     if (num2 > num1):
          print "Your second number is greater"

     if (num1 == num2):
          print "Your numbers are equal"

menuOption = raw_input("Enter another number? (y/n)> ")

Edited by The Sorrow
Link to comment
Share on other sites

menuOption = raw_input("Enter another number? (y/n)> ")

is never executed and thus, menuOption cannot be set to "n" by the user.

it prompts me if i want to enter another number....so it seems to work. Basically while menuOption is not equal to "n" it'll loop.

Edited by The Sorrow
Link to comment
Share on other sites

This is why python is fun (using whitespace as syntax)

num1 = 0
num2 = 0
menuOption = ""

print "Hello, I'm a number comparison program "
while (menuOption != "n"):
     num1 = input("Enter a number> ")
     num2 = input("Enter another number> ")

     if (num1 > num2):
          print "Your first number is greater"

     if (num2 > num1):
          print "Your second number is greater"

     if (num1 == num2):
          print "Your numbers are equal"

     menuOption = raw_input("Enter another number? (y/n)> ")

It needs to be in the while loop. Maybe it is in your editor but it failed to copy/paste correctly.

Link to comment
Share on other sites

oh got ya lol. yea, thats how the source is. im taking an intro programming class in college and was ahead so i thought id get more ahead. wowed my instructor and his TAs by using a list array to convert numbers to roman numerals :P

Link to comment
Share on other sites

This is why python is fun (using whitespace as syntax)

num1 = 0
num2 = 0
menuOption = ""

print "Hello, I'm a number comparison program "
while (menuOption != "n"):
     num1 = input("Enter a number> ")
     num2 = input("Enter another number> ")

     if (num1 > num2):
          print "Your first number is greater"

     if (num2 > num1):
          print "Your second number is greater"

     if (num1 == num2):
          print "Your numbers are equal"

     menuOption = raw_input("Enter another number? (y/n)> ")

It needs to be in the while loop. Maybe it is in your editor but it failed to copy/paste correctly.

while(1):
    #your code
    prompt for answer if done:
        break
exit(0)

remember to add error checking for the answer

Edited by SomeoneE1se
Link to comment
Share on other sites

sorry for the double post but this is that roman numeral converter i made today too.

list = ['I','II','III','IV','V','VI','VII','VIII','IX','X']
number = 0
menuOption = "" 

print "Hello, Im a roman numeral conversion program"

while (menuOption != "n"):
    number = input("Enter a number to be converted> ")
    if (number < 1):
        print "ERROR - Can't convert numbers less than or equal to zero"
    if (number > 10):
        print "ERROR - Can't convert numbers over 10"
    if (number <= 10 and number >= 1):
        print list[number - 1]

    menuOption = raw_input("Run again y/n > ")
if (menuOption == "n"):
    print "Goodbye :)"

Edited by The Sorrow
Link to comment
Share on other sites

#set vaules
list = ['I','II','III','IV','V','VI','VII','VIII','IX','X']
number = 0

#init
print "Hello, Im a roman numeral conversion program"

while (1):
    number = input("Enter a number to be converted> ")
    if (number < 1):
        print "ERROR - Can't convert numbers less than or equal to zero"
    if (number > 10):
        print "ERROR - Can't convert numbers over 10"
    if (number <= 10 and number >= 1):
        print list[number - 1]

    menuOption = raw_input("Run again y/n > ")
    if (menuOption == "n"):
    print "Goodbye :)"
    break
#end

also I would suggest 4 spaces in replacement of tabs when you're working with python.

Link to comment
Share on other sites

#set vaules
list = ['I','II','III','IV','V','VI','VII','VIII','IX','X']
number = 0
menuOption = ""

#init
print "Hello, Im a roman numeral conversion program"

while (1):
    number = input("Enter a number to be converted> ")
    if (number < 1):
        print "ERROR - Can't convert numbers less than or equal to zero"
    if (number > 10):
        print "ERROR - Can't convert numbers over 10"
    if (number <= 10 and number >= 1):
        print list[number - 1]

    menuOption = raw_input("Run again y/n > ")
    if (menuOption == "n"):
    print "Goodbye :)"
    break
#end

also I would suggest 4 spaces in replacement of tabs when you're working with python.

forgot to declare menuOption :P

Link to comment
Share on other sites

ok so its just good habbit to insert 'break' at the end of a loop

Actually it would be a bad habit to use a break like that. Use of break is generally discouraged. Essentially, a break is very similar to the infamous 'goto' and should be avoided unless necessary.

Link to comment
Share on other sites

Actually it would be a bad habit to use a break like that. Use of break is generally discouraged. Essentially, a break is very similar to the infamous 'goto' and should be avoided unless necessary.

in this case you'd be right, but if theres a complicated check required a function+break would be better

code should be easy to read, remember that and you'll do fine

Edited by SomeoneE1se
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...