Python 3.4 代码帮助 - 我无法完成这项工作
Python 3.4 code help - I can't make this work
我是 python 的新手。这是我的代码:
print("Welcome to the Currency Converter")
print("This Currency Converter converts: ")
print(" US [D]ollar")
print(" [E]uro")
print(" British[P]ound Sterling")
print(" Japanese[Y]en")
print()
def input1() :
a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
if a.lower is not ('e','y','p','d'):
print("That is not a valid currency inital. Please try again ")
input1()
input1()
def input2() :
b = input("Enter the currency inital you wish to convert to: ")
if b.lower is not ('e','y','p','d'):
print("That is not a valid currency inital. Please try again")
input2()
input2()
它总是重复,即使我不希望它重复。我的目标是使其仅在输入 e
、y
、d
或 p
时才起作用,否则它应该显示错误消息并重复问题。
a.lower
是一个你需要调用的函数,否则你只会得到一个函数引用。 a.lower()
. 也是如此
if x is not ('e', 'y', 'p', 'd')
– is
(或is not
)运算符检查身份。左侧是用户输入,右侧是一个包含可能字符的 4 元素元组。这两个人永远不会有相同的身份。您要使用 in
运算符:
if a.lower() not in ('e', 'y', 'p', 'd'):
…
您的代码没有正确调用函数,也没有为转换分配全局变量。此外,你不应该使用 is
关键字,它检查内存中的等效引用,你应该使用 in
关键字来检查元组中是否存在元素。
print("Welcome to the Currency Converter")
print("This Currency Converter converts: ")
print(" US [D]ollar")
print(" [E]uro")
print(" British[P]ound Sterling")
print(" Japanese[Y]en")
print()
def input1() :
a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
if a.lower() not in ('e','y','p','d'):
print("That is not a valid currency inital. Please try again ")
return input1()
return a
a = input1()
def input2() :
b = input("Enter the currency inital you wish to convert to: ")
if b.lower() not in ('e','y','p','d'):
print("That is not a valid currency inital. Please try again")
return input2()
return b
b = input2()
有几个问题:
您正在读取函数中的输入,但从未返回它。
def fun():
a = 1
fun()
print(a) # NameError
您的测试失败,因为您正在将方法 (a.lower
) 与元组进行比较。您必须调用该函数并检查结果是否为 in
序列类型:
if a.lower() not in ('e', 'y', 'p', 'd'):
...
终于不用递归调用input1
函数了:
def input1() :
while True:
a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
if a.lower() not in ('e','y','p','d'):
print("That is not a valid currency inital. Please try again ")
else:
return a.lower()
然后在脚本的 "main" 部分:
from_curr = input1()
(不要使用 from
作为变量名,因为它是 keyword)。
我是 python 的新手。这是我的代码:
print("Welcome to the Currency Converter")
print("This Currency Converter converts: ")
print(" US [D]ollar")
print(" [E]uro")
print(" British[P]ound Sterling")
print(" Japanese[Y]en")
print()
def input1() :
a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
if a.lower is not ('e','y','p','d'):
print("That is not a valid currency inital. Please try again ")
input1()
input1()
def input2() :
b = input("Enter the currency inital you wish to convert to: ")
if b.lower is not ('e','y','p','d'):
print("That is not a valid currency inital. Please try again")
input2()
input2()
它总是重复,即使我不希望它重复。我的目标是使其仅在输入 e
、y
、d
或 p
时才起作用,否则它应该显示错误消息并重复问题。
a.lower
是一个你需要调用的函数,否则你只会得到一个函数引用。a.lower()
. 也是如此
if x is not ('e', 'y', 'p', 'd')
–is
(或is not
)运算符检查身份。左侧是用户输入,右侧是一个包含可能字符的 4 元素元组。这两个人永远不会有相同的身份。您要使用in
运算符:if a.lower() not in ('e', 'y', 'p', 'd'): …
您的代码没有正确调用函数,也没有为转换分配全局变量。此外,你不应该使用 is
关键字,它检查内存中的等效引用,你应该使用 in
关键字来检查元组中是否存在元素。
print("Welcome to the Currency Converter")
print("This Currency Converter converts: ")
print(" US [D]ollar")
print(" [E]uro")
print(" British[P]ound Sterling")
print(" Japanese[Y]en")
print()
def input1() :
a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
if a.lower() not in ('e','y','p','d'):
print("That is not a valid currency inital. Please try again ")
return input1()
return a
a = input1()
def input2() :
b = input("Enter the currency inital you wish to convert to: ")
if b.lower() not in ('e','y','p','d'):
print("That is not a valid currency inital. Please try again")
return input2()
return b
b = input2()
有几个问题:
您正在读取函数中的输入,但从未返回它。
def fun():
a = 1
fun()
print(a) # NameError
您的测试失败,因为您正在将方法 (a.lower
) 与元组进行比较。您必须调用该函数并检查结果是否为 in
序列类型:
if a.lower() not in ('e', 'y', 'p', 'd'):
...
终于不用递归调用input1
函数了:
def input1() :
while True:
a = input("Enter the currency inital {eg. [E] for Euro} you wish to convert from: ")
if a.lower() not in ('e','y','p','d'):
print("That is not a valid currency inital. Please try again ")
else:
return a.lower()
然后在脚本的 "main" 部分:
from_curr = input1()
(不要使用 from
作为变量名,因为它是 keyword)。