The Sorrow Posted October 7, 2010 Posted October 7, 2010 (edited) 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 October 7, 2010 by The Sorrow Quote
Sparda Posted October 7, 2010 Posted October 7, 2010 while(keep_going) { if (user says no) { keep_going = false; } } Quote
The Sorrow Posted October 7, 2010 Author Posted October 7, 2010 while(keep_going) { if (user says no) { keep_going = false; } } its in python, :( should have specified Quote
Sparda Posted October 7, 2010 Posted October 7, 2010 http://www.tutorialspoint.com/python/python_while_loop.htm Quote
The Sorrow Posted October 7, 2010 Author Posted October 7, 2010 sweet, ill see if i can get it figured out ;) Quote
Mr-Protocol Posted October 7, 2010 Posted October 7, 2010 Yup, basic while loop. Pretty universal. Quote
The Sorrow Posted October 7, 2010 Author Posted October 7, 2010 (edited) 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 October 7, 2010 by The Sorrow Quote
Sparda Posted October 7, 2010 Posted October 7, 2010 menuOption = raw_input("Enter another number? (y/n)> ") is never executed and thus, menuOption cannot be set to "n" by the user. Quote
The Sorrow Posted October 7, 2010 Author Posted October 7, 2010 (edited) 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 October 7, 2010 by The Sorrow Quote
Sparda Posted October 7, 2010 Posted October 7, 2010 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. Quote
The Sorrow Posted October 7, 2010 Author Posted October 7, 2010 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 Quote
SomeoneE1se Posted October 8, 2010 Posted October 8, 2010 (edited) 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 October 8, 2010 by SomeoneE1se Quote
The Sorrow Posted October 8, 2010 Author Posted October 8, 2010 so insert a break command? im not quite sure what your referring to. Quote
Sparda Posted October 8, 2010 Posted October 8, 2010 The break operator is a way of leaving a loop at any point in the loop. In this instance it will have the same effect as using a variable to control the loop condition. Quote
The Sorrow Posted October 8, 2010 Author Posted October 8, 2010 ok so its just good habbit to insert 'break' at the end of a loop Quote
The Sorrow Posted October 8, 2010 Author Posted October 8, 2010 (edited) 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 October 8, 2010 by The Sorrow Quote
SomeoneE1se Posted October 8, 2010 Posted October 8, 2010 #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. Quote
The Sorrow Posted October 8, 2010 Author Posted October 8, 2010 #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 Quote
Sitwon Posted October 9, 2010 Posted October 9, 2010 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. Quote
SomeoneE1se Posted October 9, 2010 Posted October 9, 2010 (edited) 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 October 9, 2010 by SomeoneE1se Quote
The Sorrow Posted October 9, 2010 Author Posted October 9, 2010 Thanks for the clarification guys :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.