Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

python help: how do i prevent an infinite loop?

OpenStudy (anonymous):

``` print('1. Argentine Peso') print('2. Brazilian Real') print('3. British Pound') print('4. European Euro') print('5. Indian Rupee') print('6. Japanese Yen') print('7. Russian Ruble') print('8. Other \n') usd = 0 usd = eval(input('Enter amount to convert (=< 0 to exit): ')) peso = usd * 8.48 real = usd * 2.46 pound = usd * 0.62 euro = usd * 0.79 rupee = usd * 61.59 yen = usd * 109.49 ruble = usd * 39.45 num = 0 num = eval(input('Enter Selection: ')) while num > 0: if num == 1: print('' + str(usd) + ' US Dollars converts to: ' + str(round(peso,2)) + ' Argentine Pesos.') elif num == 2: print('' + str(usd) + ' US Dollars converts to: ' + str(round(real,2)) + ' Brazilian Real.') elif num == 3: print('' + str(usd) + ' US Dollars converts to: ' + str(round(pound,2)) + ' British Pound.') elif num == 4: print('' + str(usd) + ' US Dollars converts to: ' + str(round(euro,2)) + ' European Euro.') elif num == 5: print('' + str(usd) + ' US Dollars converts to: ' + str(round(rupee,2)) + ' Indian Rupee.') elif num == 6: print('' + str(usd) + ' US Dollars converts to: ' + str(round(yen,2)) + ' Japanese Yen.') elif num == 7: print('' + str(usd) + ' US Dollars converts to: ' + str(ruble) + ' Russian Ruble.') elif num == 8: currency = input('Enter current to convert to: ') amount = eval(input('Enter the conversion factor for ' + currency + ': ')) random = usd * amount print('' + str(usd) + ' US Dollars converts to: ' + str(random) + ' ' + currency + '.') ```

OpenStudy (anonymous):

this is my code

OpenStudy (anonymous):

its my first assignment using a while loop

OpenStudy (bibby):

if you want, you can debug the code by stepping through it but the basic idea you're not getting is the logic of a while loop. ``` while(condition){ //code //update } ``` in your code num never gets modified so it's always > 0 no matter what

OpenStudy (bibby):

tbh I can't even tell why you're using a while loop there

OpenStudy (anonymous):

hm so in each if statement i need to add something like num = num + 1 ? we have to use a while loop because it needs to restart again after it completes one conversion.

OpenStudy (bibby):

num++; should offer the same functionality. that's a weird program tho

OpenStudy (anonymous):

yeah its kinda strange, idk just doing my assignments lol ill have to use num = num + 1 i think cause python doesnt have that ability to use num++

OpenStudy (bibby):

sorry I'm tired as he*k

OpenStudy (anonymous):

oh no not a problem, i understand

OpenStudy (anonymous):

feel free to get off haha, i will figure it out eventually

OpenStudy (bibby):

count += 1; should work

OpenStudy (bibby):

lol no way, I have my own assignments to do. but the internet is more interesting :/

OpenStudy (anonymous):

hahahah i hear that

OpenStudy (bibby):

so basically if you enter 1 it goes through the menu from 1-8?

OpenStudy (bibby):

but if you enter 6 it only does 6-8?

OpenStudy (anonymous):

ok ill explain

OpenStudy (anonymous):

you enter say '50' for usd then select one number from 1-8 and it only does the one selected. then it should ask you to enter a value for usd again

OpenStudy (anonymous):

right now it does what you said, if you enter 5 it runs 6,7,8 but i need it to only run the selected option

OpenStudy (harsha19111999):

You can use the break function

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!