line shift? (baby steps 2)

A

Artemisio

I have done a small currency calculator. It works and I'm very glad.
But...I'd like to have a line shift if user types a wrong choice.
Please, look at the code and output example down here:

# -*- coding: ISO-8859-1 -*-
import string

eudk_currency= 7.47
dkeu_currency= 0.13
usdk_currency= 5.93
dkus_currency= 0.16
euus_currency= 1.19
useu_currency= 0.78

currency_choice= raw_input("Please type DK if you want convert to DKK,
EU if you want to convert to €: ")
currency_str= str(currency_choice).upper()

while currency_str != "EUDK" and currency_str != "DKEU" and
currency_str != "USDK" and currency_str != "DKUS"\
and currency_str != "EUUS" and currency_str!= "USEU":
currency_choice= raw_input("Sorry. At the moment we only support
DKK, Euro and Dollars." + "\n" \
"Type DKEU if you want convert from DKK to €," + "\n" \
"EUDK if you want to convert from DKK to €," + "\n" \
"DKUS if you want to convert from DKK to $," + "\n" \
"USDK if you want to convert from $ to DKK," + "\n"
"EUUS if you want to convert from € to $," + "\n"\
"and USEU if you want to convert from $ to €: ")
currency_str= str(currency_choice).upper()

amount_int= input("Please type the amount you wish to convert: ")

if currency_str == "DKEU":
result= amount_int* dkeu_currency
print amount_int, "Danish Crowns correspond to", result, "Euro."

if currency_str == "EUDK":
result= amount_int* eudk_currency
print amount_int, "Euro correspond to", result, "Danish Crowns."

if currency_str == "USDK":
result= amount_int* usdk_currency
print amount_int, "Dollars correspond to", result, "Danish Crowns."

if currency_str == "DKUS":
result= amount_int* dkus_currency
print amount_int, "Danish Crowns correspond to", result, "Dollars."

if currency_str == "EUUS":
result= amount_int* euus_currency
print amount_int, "Euro correspond to", result, "Dollars."

if currency_str == "USEU":
result= amount_int* useu_currency
print amount_int, "Dollars correspond to", result, "Euro."
#CODE END

OUTPUT:

Please type DK if you want convert to DKK, EU if you want to convert
to €: sdsd
Sorry. At the moment we only support DKK, Euro and Dollars.
Type DKEU if you want convert from DKK to €,
EUDK if you want to convert from DKK to €,
DKUS if you want to convert from DKK to $,
USDK if you want to convert from $ to DKK,
EUUS if you want to convert from € to $,
and USEU if you want to convert from $ to €: usdk
Please type the amount you wish to convert: 99
99 Dollars correspond to 587.07 Danish Crowns.

How do I get a line shift before (and/or) after the "Sorry....At the
moment"), so the output is something like:

Please type DK if you want convert to DKK, EU if you want to convert
to €: sdsd

Sorry. At the moment we only support DKK, Euro and Dollars.
Type DKEU if you want convert from DKK to €,
EUDK if you want to convert from DKK to €,
DKUS if you want to convert from DKK to $,
USDK if you want to convert from $ to DKK,
EUUS if you want to convert from € to $,
and USEU if you want to convert from $ to €: usdk
Please type the amount you wish to convert: 99
99 Dollars correspond to 587.07 Danish Crowns.

OR

Please type DK if you want convert to DKK, EU if you want to convert
to €: sdsd

Sorry. At the moment we only support DKK, Euro and Dollars.

Type DKEU if you want convert from DKK to €,
EUDK if you want to convert from DKK to €,
DKUS if you want to convert from DKK to $,
USDK if you want to convert from $ to DKK,
EUUS if you want to convert from € to $,
and USEU if you want to convert from $ to €: usdk
Please type the amount you wish to convert: 99
99 Dollars correspond to 587.07 Danish Crowns.

???
Thank you for your patience! :)
 
A

Andrew Durdin

I have done a small currency calculator. It works and I'm very glad.
But...I'd like to have a line shift if user types a wrong choice.

A simple "print" with no arguments should display the blank line you want:

while currency_str != "EUDK" and currency_str != "DKEU" and
currency_str != "USDK" and currency_str != "DKUS"\
and currency_str != "EUUS" and currency_str!= "USEU":
print
currency_choice= raw_input("Sorry. At the moment we only support
DKK, Euro and Dollars." + "\n" \
"Type DKEU if you want convert from DKK to €," + "\n" \
"EUDK if you want to convert from DKK to €," + "\n" \
"DKUS if you want to convert from DKK to $," + "\n" \
"USDK if you want to convert from $ to DKK," + "\n"
"EUUS if you want to convert from € to $," + "\n"\
"and USEU if you want to convert from $ to €: ")
currency_str= str(currency_choice).upper()

Or you can add an extra "\n" at the beginning of your "Sorry" string.
 
R

Reinhold Birkenfeld

Artemisio said:
I have done a small currency calculator. It works and I'm very glad.
But...I'd like to have a line shift if user types a wrong choice.
Please, look at the code and output example down here:

# -*- coding: ISO-8859-1 -*-
import string

eudk_currency= 7.47
dkeu_currency= 0.13
usdk_currency= 5.93
dkus_currency= 0.16
euus_currency= 1.19
useu_currency= 0.78

As a hint not pertaining to your actual problem: Use a dictionary to
store the values, like this:

currencies = { "EUDK" : 7.47, "DKEU" : 0.13, (...) }

Then you do not have to do if statements like this

if currency_str == "DKEU":
result= amount_int* dkeu_currency
print amount_int, "Danish Crowns correspond to", result, "Euro."

(...)

You can directly use the input string as index into the dictionary:

result = amount_int * currencies[currency_str]
print (...)

Reinhold
 
J

Jeff Lindholm

As a hint not pertaining to your actual problem: Use a dictionary to
store the values, like this:

currencies = { "EUDK" : 7.47, "DKEU" : 0.13, (...) }

Then you do not have to do if statements like this

if currency_str == "DKEU":
result= amount_int* dkeu_currency
print amount_int, "Danish Crowns correspond to", result, "Euro."

(...)

You can directly use the input string as index into the dictionary:

result = amount_int * currencies[currency_str]
print (...)
I was bored waiting on a compile so I hacked this up. It adds the \n and the
Please enter conversion type line and uses the previous poster's comment on
the using a dictionary.
I added the conversion output text by makeing the value a list that is the
conversion and the text to display for the answer (I made them shorter
because I was lazy to type all the information). I also made the conversion
from US to EU more in line with the EUUS because I used those for testing
and the results were not coming out the same (course I am sure there is some
slack in there somewhere for the bank taking its share of the proceeds :) )
Not sure if there is a slicker way to do the conversion/message - I am
relatively new to Python as well, so my code still ends up looking like C++
lots of the time.

import string

currency = {"EUDK":(7.47,"? to DKK"),
"DKEU":(0.13,"DKK to ?"),
"USDK":(5.93, "$ to DKK"),
"DKUS":(0.16, "DKK to $"),
"EUUS":(1.19, "? to $"),
"USEU":(0.84, "$ to ?")}

while(1):
currency_choice= raw_input("\nPlease enter conversion type or 'Exit':
")
currency_str= str(currency_choice).upper()

if currency_str == "EXIT":
break
if currency.has_key(currency_str):
amount_int= input("Please type the amount you wish to convert: ")
result = amount_int * currency[currency_str][0]
print currency[currency_str][1], " ", amount_int, " = ", result
else:
print("Sorry. At the moment we only support DKK, Euro and Dollars."
+ "\n" \
"Type DKEU if you want convert from DKK to ?," + "\n" \
"EUDK if you want to convert from DKK to ?," + "\n" \
"DKUS if you want to convert from DKK to $," + "\n" \
"USDK if you want to convert from $ to DKK," + "\n"
"EUUS if you want to convert from ? to $," + "\n"\
"and USEU if you want to convert from $ to ?")
 
A

Artemisio

Reinhold Birkenfeld said:
Artemisio said:
I have done a small currency calculator. It works and I'm very glad.
But...I'd like to have a line shift if user types a wrong choice.
Please, look at the code and output example down here:

# -*- coding: ISO-8859-1 -*-
import string

eudk_currency= 7.47
dkeu_currency= 0.13
usdk_currency= 5.93
dkus_currency= 0.16
euus_currency= 1.19
useu_currency= 0.78

As a hint not pertaining to your actual problem: Use a dictionary to
store the values, like this:

currencies = { "EUDK" : 7.47, "DKEU" : 0.13, (...) }

Then you do not have to do if statements like this

if currency_str == "DKEU":
result= amount_int* dkeu_currency
print amount_int, "Danish Crowns correspond to", result, "Euro."

(...)

You can directly use the input string as index into the dictionary:

result = amount_int * currencies[currency_str]
print (...)

Reinhold

Great! Thank you pals! I'll have a closer look to your suggestion
Reinhold, it seems a slick way to do the same thing
 
A

Artemisio

I was bored waiting on a compile so I hacked this up. It adds the \n and the
Please enter conversion type line and uses the previous poster's comment on
the using a dictionary.
I added the conversion output text by makeing the value a list that is the
conversion and the text to display for the answer (I made them shorter
because I was lazy to type all the information). I also made the conversion
from US to EU more in line with the EUUS because I used those for testing
and the results were not coming out the same (course I am sure there is some
slack in there somewhere for the bank taking its share of the proceeds :) )
Not sure if there is a slicker way to do the conversion/message - I am
relatively new to Python as well, so my code still ends up looking like C++
lots of the time.

import string

currency = {"EUDK":(7.47,"? to DKK"),
"DKEU":(0.13,"DKK to ?"),
"USDK":(5.93, "$ to DKK"),
"DKUS":(0.16, "DKK to $"),
"EUUS":(1.19, "? to $"),
"USEU":(0.84, "$ to ?")}

while(1):
currency_choice= raw_input("\nPlease enter conversion type or 'Exit':
")
currency_str= str(currency_choice).upper()

if currency_str == "EXIT":
break
if currency.has_key(currency_str):
amount_int= input("Please type the amount you wish to convert: ")
result = amount_int * currency[currency_str][0]
print currency[currency_str][1], " ", amount_int, " = ", result
else:
print("Sorry. At the moment we only support DKK, Euro and Dollars."
+ "\n" \
"Type DKEU if you want convert from DKK to ?," + "\n" \
"EUDK if you want to convert from DKK to ?," + "\n" \
"DKUS if you want to convert from DKK to $," + "\n" \
"USDK if you want to convert from $ to DKK," + "\n"
"EUUS if you want to convert from ? to $," + "\n"\
"and USEU if you want to convert from $ to ?")

Thank you Jeff, that's classy! Also the exit function! Your hack will
keep me busy for at least one hour and a half (hence the mentioned
baby steps)

Allow me a remark though:

was the following output intended?

"Please enter conversion type or 'Exit': euus
Please type the amount you wish to convert: 99
? to $ 99 = 117.81"

I mean the last line. Or am I missing something?
Thanx!
 
J

Jeff Lindholm

import string
currency = {"EUDK":(7.47,"? to DKK"),
"DKEU":(0.13,"DKK to ?"),
"USDK":(5.93, "$ to DKK"),
"DKUS":(0.16, "DKK to $"),
"EUUS":(1.19, "? to $"),
"USEU":(0.84, "$ to ?")}

while(1):
currency_choice= raw_input("\nPlease enter conversion type or
'Exit':
")
currency_str= str(currency_choice).upper()

if currency_str == "EXIT":
break
if currency.has_key(currency_str):
amount_int= input("Please type the amount you wish to convert: ")
result = amount_int * currency[currency_str][0]
print currency[currency_str][1], " ", amount_int, " = ", result
else:
print("Sorry. At the moment we only support DKK, Euro and
Dollars."
+ "\n" \
"Type DKEU if you want convert from DKK to ?," + "\n" \
"EUDK if you want to convert from DKK to ?," + "\n" \
"DKUS if you want to convert from DKK to $," + "\n" \
"USDK if you want to convert from $ to DKK," + "\n"
"EUUS if you want to convert from ? to $," + "\n"\
"and USEU if you want to convert from $ to ?")


was the following output intended?

"Please enter conversion type or 'Exit': euus
Please type the amount you wish to convert: 99
? to $ 99 = 117.81"

I mean the last line. Or am I missing something?
Thanx!

Yes the last line was meant to show the conversion type, just a simple
remider for myself what I converted (in this case EU - designated ? - to US
dollars - designated $)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top