Avatar billede schipa Nybegynder
23. august 2004 - 08:09 Der 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.

Tak på forhånd!
Avatar billede squashguy Nybegynder
23. august 2004 - 08:52 #1
if currency_str == "dk":
elif currency_str == "eu":
else:

--

hvis du vil bruge din elif, skal den nok skrives som:

elif currency_str != "dk" and currency_str != "eu":
Avatar billede schipa Nybegynder
23. august 2004 - 09:21 #2
squashguy: jeg har prøvet dit forslag:

# -*- 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?
Avatar billede alvion Nybegynder
23. august 2004 - 10:09 #3
# -*- coding: ISO-8859-1 -*-
euro_currency= 7.47
dk_currency= 0.13

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

    elif currency_str == "eu":
        result= amount_int* dk_currency   
        print amount_int, "Danish Crown correspond to", result, "Euro"
        break
Avatar billede alvion Nybegynder
23. august 2004 - 10:09 #4
Hvis jeg har fors

Prøv med ovenstånende loop
Avatar billede alvion Nybegynder
23. august 2004 - 10:36 #5
Det gik ikke så godt :-) Jeg prøver igen

"Hvis jeg har forstået dig rigtigt, så vil du have beregneren til at "starte forfra", hvis man indtaster noget andet end dk og eu?

Prøv med et loop"
Avatar billede alvion Nybegynder
23. august 2004 - 10:38 #6
Eller endnu bedre:


# -*- coding: ISO-8859-1 -*-
euro_currency= 7.47
dk_currency= 0.13

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"

elif currency_str == "eu":
    result= amount_int* dk_currency   
    print amount_int, "Danish Crown correspond to", result, "Euro"
Avatar billede schipa Nybegynder
23. august 2004 - 10:39 #7
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...
Avatar billede schipa Nybegynder
23. august 2004 - 10:43 #8
heeej! Alvion, nu har du det rigtigt.

Kan du fortælle mig, hvad jeg gjorde forkert?
Og opretter du et svar, så får du points?
Takker max!
Avatar billede schipa Nybegynder
23. august 2004 - 10:45 #9
det er nok, at linjen:

'currency_str= str(currency_choice)'

står både dedented og så indented igen til sidst for while loop?
Avatar billede alvion Nybegynder
23. august 2004 - 10:54 #10
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.
Avatar billede schipa Nybegynder
23. august 2004 - 11:24 #11
mange tak!

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).

funktionen er vist:

import string
currency_str = string.upper(currency_choice);   

men hvor skal den være?
Avatar billede alvion Nybegynder
23. august 2004 - 11:44 #12
import string

euro_currency= 7.47
dk_currency= 0.13

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"

elif currency_str == "EU":
    result= amount_int* dk_currency   
    print amount_int, "Danish Crown correspond to", result, "Euro"


Det er noget af det elegante ved Python. Objekter der returneres fra funktioner kan refereres direkte.
Avatar billede schipa Nybegynder
23. august 2004 - 11:59 #13
det var skrapt, alvion!

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!
Avatar billede Ny bruger Nybegynder

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester