Python if else程序

2024-10-02 14:20:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个选择题程序,它的表现和我期望的不一样。你知道吗

我想让它这样-

  1. 键入1时,转到#USD下面的块并
  2. 当你输入2时,它应该在欧元区下方

这是我的代码:

print "Welcome to Currency Converter By Fend Artz"
print "your options are:"
print " "
print "1) GDP -> USD"
print "2) GDP -> Euro"
USD = int(raw_input(''))
if USD == 1:
    choice = USDchoice    
elif USD == 2:
    choice = EUROchoice
else:
    print ("You have to put a number 1 or 2")
    int(raw_input(''))
#USD
def USDchoice():
    userUSD = float(input('How many pounds do you want to convert?(e.g. 5)\n'))
    USD = userUSD * 0.65
    print userUSD, "Pounds =",USD,"USDs"

#Euro
def EUROchoice():
    userEURO = float(input('How many pounds do you want to convert?(e.g. 5)\n'))
    Euro = userEURO * 1.37 
    print userEURO, "Pounds =",Euro,"Euros"

#Thing so the script doesn't instantly close
Enter = raw_input('press ENTER to close\n')

Tags: toinputrawdeffloatintusdprint
1条回答
网友
1楼 · 发布于 2024-10-02 14:20:19

代码有两个问题。你知道吗

  1. 将变量choice设置为对函数之一的引用:USDChoiceEUROChoice。您需要调用这些函数,使用括号将变量设置为它们返回的值。正如一些评论所指出的那样,您可以像USDChoice()EUROChoice()这样做。你知道吗
  2. 您尝试在创建函数之前调用它们。它们需要移到上面,因为所有内容都在全局范围内(模块级)。你知道吗

固定代码:

#USD
def USDchoice():
    userUSD = float(input('How many pounds do you want to convert?(e.g. 5)\n'))
    USD = userUSD * 0.65
    print userUSD, "Pounds =",USD,"USDs"


#Euro
def EUROchoice():
    userEURO = float(input('How many pounds do you want to convert?(e.g. 5)\n'))
    Euro = userEURO * 1.37 
    print userEURO, "Pounds =",Euro,"Euros"


print "Welcome to Currency Converter By Fend Artz"
print "your options are:"
print " "
print "1) GDP -> USD"
print "2) GDP -> Euro"
USD = int(raw_input(''))

if USD == 1:
    choice = USDchoice()
elif USD == 2:
    choice = EUROchoice()
else:
    print ("You have to put a number 1 or 2")
    int(raw_input(''))

#Thing so the script doesn't instantly close
Enter = raw_input('press ENTER to close\n')

相关问题 更多 >