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

Please, is anyone here familiar with Python Version 3.4.1? @ Anyone at all who can help me, please help, my assignment is due in 4 hours 30 minutes and ive been stuck witht he same problem for 4 days already

OpenStudy (anonymous):

My code so far ``` 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 print('-----------------------------------------------------------') 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: num = num + 1 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 + '.') else: print('-----------------------------------------------------------') #mark of starting over eval(input('Enter amount to convert (=<0 to exit): ')) num = eval(input('Enter selection: ')) ```

OpenStudy (anonymous):

It works pretty well so far but right now it after you select a number 1-8, it adds 1 to your selection and prnts all after that one. Example: Selection is 4, the program runs selection #s 5,6,7,8 and restarts the program

OpenStudy (anonymous):

@e.mccormick hey so i know ive already asked for you to help with this exact problem but i still cant figure out what the problem is. any chance you would be willing to help again :\

OpenStudy (e.mccormick):

Well, you are adding to the num... =/

OpenStudy (anonymous):

someone told me that my problem has to do with using ``` num=num+1 ``` i tried many things but idk, i need it because its the only thing i could find to do to fix the infinite loop problem i had before

OpenStudy (e.mccormick):

Well, are you trying to offer the person a chance to convert again, or to a different ammount of money?

OpenStudy (anonymous):

yes im trying to allow the user to choose one conversion after entering an amount for usd, then after that one conversion it should ask for a new value for usd and allow them to pick a new conversion option.

OpenStudy (e.mccormick):

Then put the user input inside the loop and also tell them to put in 0 to exit.

OpenStudy (anonymous):

This is my actualy assignment instruction paper if you wanna look quick

OpenStudy (e.mccormick):

Your loop is also evaluation on the wrong value.

OpenStudy (anonymous):

what do you mean by that?

OpenStudy (e.mccormick):

'while num > 0:' Instructions: Write a program to continually ask the user for a US dollar amount to be converted to a monetary value in some other currency. Terminate the program when the user enters zero(0) or a negative value. num is not the monetary value.

OpenStudy (anonymous):

usd is the monetary value?

OpenStudy (e.mccormick):

Yes. USD is the monitary value Also, think about this: ``` flag = true while flag: if input <= 0: flag = false other stuff ``` That is one possible solution. Or doing if and a break/continue and testing on usd > 0.

OpenStudy (e.mccormick):

A do-while loop is another possibility because that does stuff at least once.

OpenStudy (anonymous):

yeah another person suggested the do-while loop but i dont believe we learned about those just yet

OpenStudy (anonymous):

as far as loops go, we learned while and for loops

OpenStudy (anonymous):

but only the basic while loop

OpenStudy (e.mccormick):

They have it end right after the curency is set to 0, so you need to use an if-break construct.

OpenStudy (anonymous):

if i put the piece where user inputs usd value and makes a selection, inside the while loop, all it prints are #1-8 selections and program ends

OpenStudy (e.mccormick):

You just need to make it true before they input the amount. You made it false.

OpenStudy (e.mccormick):

Also, watch out for where your calculations happen.

OpenStudy (anonymous):

``` 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 ``` should this be inside the while?

OpenStudy (e.mccormick):

Yep. You need to recalculate after a new value it taken in and before the print of the new output.

OpenStudy (anonymous):

alright so i think its working, is my else statement at the bottom just totally pointless?

OpenStudy (e.mccormick):

No. It is incorrect, but not pointless. You would use an else there for an error message if they entered an invalid type of currency.

OpenStudy (anonymous):

ooooh so its correct but i should make it print something else.

OpenStudy (anonymous):

i think ill just remove it, an error message isnt required for this and i dont need to complicate things at this point lol

OpenStudy (e.mccormick):

It is good to have an else as a catch all. No need to have any input. Just output for "What where you thinking!"

OpenStudy (anonymous):

oh so its that easy, alright

OpenStudy (anonymous):

See any problems? ``` 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): ')) while usd > 0: 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: ')) 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 + '.') else: print('What were you thinking!') ```

OpenStudy (anonymous):

ive tested each selection and seems to work

OpenStudy (e.mccormick):

Well, might want a nice one like, "That is not a valid currency selection" but it works. Try it for like 9 or 42. See, that is where the else is handy.

OpenStudy (e.mccormick):

Calculate after getting the new ... ummm... you still do not have it looping validly.

OpenStudy (e.mccormick):

Try and get it to do the same output as in the examples.

OpenStudy (anonymous):

alright

OpenStudy (anonymous):

i tried to put the usd part inside the loop and it gives me an error or it just prints out the option list

OpenStudy (e.mccormick):

Just as a psudocode example: ``` a = 1 while a > 0 input a if a <= 0 exit loop input b print a + b ``` Because I set a to a true value, I got into the loop fine. I never actually use this value because I instantly replace it with user input. Then to make sure I have a catch to kill things, I do an if right after I get a.

OpenStudy (anonymous):

so 'a' is my 'usd' correct?

OpenStudy (anonymous):

sorry ik we already went over this part, just clarrifying

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 = 1 while usd > 0: usd = eval(input('Enter amount to convert (less than 0 to exit): ')) if usd <= 0: #used to exit if a negative number or 0 is entered. 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: ')) if num == 1: print('' + str(usd) + ' US Dollars converts to: ' + str(round(peso,2)) + ' Argentine Pesos.\n') elif num == 2: print('' + str(usd) + ' US Dollars converts to: ' + str(round(real,2)) + ' Brazilian Real.\n') elif num == 3: print('' + str(usd) + ' US Dollars converts to: ' + str(round(pound,2)) + ' British Pound.\n') elif num == 4: print('' + str(usd) + ' US Dollars converts to: ' + str(round(euro,2)) + ' European Euro.\n') elif num == 5: print('' + str(usd) + ' US Dollars converts to: ' + str(round(rupee,2)) + ' Indian Rupee.\n') elif num == 6: print('' + str(usd) + ' US Dollars converts to: ' + str(round(yen,2)) + ' Japanese Yen.\n') elif num == 7: print('' + str(usd) + ' US Dollars converts to: ' + str(ruble) + ' Russian Ruble.\n') 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 + '.\n') else: print('Invalid input! Please try again.\n') ``` idk why it all turned red but i think this is what your meaning by your example

OpenStudy (e.mccormick):

In your case, yes. See how that keeps it from falling out of the loop?

OpenStudy (anonymous):

i never knew we had an 'exit' idk what its called, a function or something?

OpenStudy (e.mccormick):

Well, exit is not how to stop a loop. Did your book/class talk about how to skip or exit loops?

OpenStudy (anonymous):

no we never really went over that

OpenStudy (anonymous):

oh nvm that doesnt even make it exit, forgot to test it.

OpenStudy (anonymous):

ill have to read into that part

OpenStudy (anonymous):

ahh so 'break' makes it exit, see we had that in c++ i hadnt thought to try that on here. i took a c++ class a few years back in high school.

OpenStudy (anonymous):

hm it exits for zero but not a negative number

OpenStudy (e.mccormick):

Odd...

OpenStudy (anonymous):

when i put a negative number it refers to the else and says invalid input and restarts

OpenStudy (e.mccormick):

Did you do == 0 or <= 0? Did you put the negative as the currency type or as the money?

OpenStudy (anonymous):

<= 0

OpenStudy (anonymous):

the currency type

OpenStudy (anonymous):

lol alright i see where your going

OpenStudy (anonymous):

the exit is if the usd = 0 or negative, so i have to do for the money not type

OpenStudy (e.mccormick):

So, was it when you tested it you put it in the wrong place? =P Thought that might have been it.

OpenStudy (anonymous):

haha yep

OpenStudy (anonymous):

alright if you dont mind ive got one more question

OpenStudy (anonymous):

its a quick one im sure ``` print('' + str(usd) + ' US Dollars converts to: ' + str(round(peso,2)) + ' Argentine Pesos.\n') ``` is there a better way to round to the 100th place (2 decimal place : 0.00) because this method doesnt work quite every time ive noticed

OpenStudy (anonymous):

hm maybe it does now, i noticed earlier it wasnt round it to two decimal places when the answer was something like 6.00 it would just put 6.0

OpenStudy (e.mccormick):

String format. Number format... hmm... that is a Java concept. The round might have an extra option.

OpenStudy (anonymous):

hm alright

OpenStudy (anonymous):

i may just turn it in like this haha, i think ive put more than what was expected to be required into this assignment lol

OpenStudy (e.mccormick):

Looks like they have string format too. Thought so.

OpenStudy (anonymous):

yeah i see it too

OpenStudy (anonymous):

thanks that should help

OpenStudy (e.mccormick):

Ah, a cleaner example then the entire format docs: http://stackoverflow.com/questions/2389846/python-decimals-format

OpenStudy (e.mccormick):

Oooh. And really good examples: http://mkaz.com/2012/10/10/python-string-format/

OpenStudy (anonymous):

oh wow that last one is fantastic! thank you! bookmarking that one

OpenStudy (anonymous):

youve got no idea how much i appreciate your help over the past few days :D

OpenStudy (e.mccormick):

Well, that is the hope of the site. Give you someone to talk to that knows a bit more than you do so you can get a few nudges in the right direction.

OpenStudy (anonymous):

yep :D i hope ill be able to help some more people the way you do for me soon. really appreciate it, now i havent slept in about 28hrs so im going sleep haha. night. have a wonderful day/night! thanks again!

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!