23. august 2004 - 08:09Der er
12 kommentarer og 1 løsning
Hjælp til begynder (Python)
Hej, jeg er i gang med at lære noget Python. Jeg er i gang med at lave en lille ting som omregner DKK til EURO og omvendt.
-*- coding: ISO-8859-1 -*- 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) euro_currency= 7.47 dk_currency= 0.13
if currency_str == "dk": amount_int= input("Please type the amount you wish to convert: ") result= amount_int* euro_currency print amount_int, "Euro correspond to", result, "Danih Crowns"
if currency_str == "eu": amount_int= input("Please type the amount you wish to convert: ") result= amount_int* dk_currency print amount_int, "Danish Crown correspond to", result, "Euro"
elif currency_str != "dk" or "eu": currency_choice= raw_input("Sorry. At the moment we only support DKK and EURO." + "\n" \ "Type DK if you want convert to DKK, EU if you want to convert to €: ")
Problemet er, at
"Sorry. At the moment we only support DKK and EURO. Type DK if you want convert to DKK, EU if you want to convert to €: "
kommer med ligegyldigt om man har valgt DK eller EU, hvilket det netop ikke burde gøre. Kan du finde fejlen?
Også, jeg havde forsøgt mig med en UPPERCASE (import string...) af den der currency string, men jeg måtte pille det ned, fordi det fungerede ikke.
# -*- coding: ISO-8859-1 -*- 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) euro_currency= 7.47 dk_currency= 0.13
if currency_str == "dk": amount_int= input("Please type the amount you wish to convert: ") result= amount_int* euro_currency print amount_int, "Euro correspond to", result, "Danih Crowns"
elif currency_str == "eu": amount_int= input("Please type the amount you wish to convert: ") result= amount_int* dk_currency print amount_int, "Danish Crown correspond to", result, "Euro"
else: currency_choice= raw_input("Sorry. At the moment we only support DKK and EURO." + "\n" \ "Type DK if you want convert to DKK, EU if you want to convert to €: ")
Det der sker er, at man ikke får det dumme "Sorry at the moment...." mere. Men det virker kun hvis man taster korrekt (DK eller EU) første gang. Taster man noget tredje, da kører det bare i ringe. Ideas?
while 1: 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)
if currency_str != "dk" and currency_str != "eu": currency_choice= raw_input("Sorry. At the moment we only support DKK and EURO." + "\n" \ "Type DK if you want convert to DKK, EU if you want to convert to €: ") continue
amount_int= input("Please type the amount you wish to convert: ") if currency_str == "dk": result= amount_int* euro_currency print amount_int, "Euro correspond to", result, "Danih Crowns" break
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) while currency_str != "dk" and currency_str != "eu": currency_choice= raw_input("Sorry. At the moment we only support DKK and EURO." + "\n" \ "Type DK if you want convert to DKK, EU if you want to convert to €: ") currency_str= str(currency_choice)
amount_int= input("Please type the amount you wish to convert: ") if currency_str == "dk": result= amount_int* euro_currency print amount_int, "Euro correspond to", result, "Danish Crowns"
Hej alvion, nice try! Men det gør tingene værre.... :-) Taster du korrekt, DK eller EU, da bliver programmet ved med at svare "please type.....". Taster du forkert, får du først den rigtige statement" Sorry...at the moment"....men så taster du DK eller EU, da bliver det ved med at køre i ringe. Prøv selv...
Ja jeg bruger VIM som editor. Den laver pr. default tab-indryk, som jeg så prøvede at rette til mellemrum inden jeg copy/pastede herover. Og det gik nok galt... ;-)
Problemet med din kode var bla., at efter beskeden om forkert valuta, så nåede programafviklingen til enden slutningen af programmet - og afsluttede selvfølgelig.
godt, jeg har den rigtige kode herinde, for jeg ødelagde det hele igen da jeg forsøgte at indbygge uppercase-tjek (altså, lave currency_choice om til uppercase).
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 != "DK" and currency_str != "EU": currency_choice= raw_input("Sorry. At the moment we only support DKK and EURO." + "\n" \ "Type DK if you want convert to DKK, EU if you want to convert to €: ") currency_str= str(currency_choice).upper()
amount_int= input("Please type the amount you wish to convert: ") if currency_str == "DK": result= amount_int* euro_currency print amount_int, "Euro correspond to", result, "Danish Crowns"
Dèt der med 'import string' og upper() havde jeg ikke regnet ud før om et år. Hvordan er du kommet til Python? Og hvor længe har du puslet med det?
Jeg er absolut ikke programmør, jeg er mere til webgrafik. Min Python-historie er, at jeg forsøgte at lære noget VBA ved selvstudium for nogle måneder siden. Det gik ikke helt så godt, så jeg stoppede halvvejs i IDG-hæftet (Programmør på en weekend(bare ikke mig)). Så blev jeg enig med mig selv om, at jeg måtte lære mig noget PHP efter at have installeret Mambo og CPG-Nuke på to af mine hjemmesider, og eftersom PHP angiveligt ikke er et optimalt valg som begynder-sprog, da måtte jeg starte med noget lettere, og jeg var så heldig at jeg stødte i Python. Det er kun en begyndelse, men jeg kan virkelig godt lide sproget!
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.