python help: how do i prevent an infinite loop?
``` 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 + '.') ```
this is my code
its my first assignment using a while loop
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
tbh I can't even tell why you're using a while loop there
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.
num++; should offer the same functionality. that's a weird program tho
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++
sorry I'm tired as he*k
oh no not a problem, i understand
feel free to get off haha, i will figure it out eventually
count += 1; should work
lol no way, I have my own assignments to do. but the internet is more interesting :/
hahahah i hear that
so basically if you enter 1 it goes through the menu from 1-8?
but if you enter 6 it only does 6-8?
ok ill explain
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
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
You can use the break function
Join our real-time social learning platform and learn together with your friends!