Python 名称错误:名称';鱼';没有定义
我是Python的新手,正在努力找出我所面临的问题。我收到以下错误消息:Python 名称错误:名称';鱼';没有定义,python,Python,我是Python的新手,正在努力找出我所面临的问题。我收到以下错误消息: 16 return output 17 ---> 18 print(fishstore(fish, price)) 19 20 NameError: name 'fish' is not defined 我正在编写的脚本: def fishname(): user_input=input("Enter Name: ") return (user
16 return output
17
---> 18 print(fishstore(fish, price))
19
20
NameError: name 'fish' is not defined
我正在编写的脚本:
def fishname():
user_input=input("Enter Name: ")
return (user_input.title())
def number():
number_input=input("Enter Price: ")
return number_input
def fishstore(fish, price):
fish_entry = fishname()
price_entry = number()
output = "Fish Type: " + fish_entry + ", costs $" + price_entry
return output
print(fishstore(fish, price))
有人能解释一下我遗漏了什么吗
先谢谢你
谢谢大家的帮助。所以我做了些工作,做了些改变
def fishstore(fish, price):
output = "Fish Type: " + fish + ", costs $" + price
return output
fish_entry = input("Enter Name: ")
fish = fish_entry
price_entry = input("Enter Price: ")
price = price_entry
print(fishstore(fish, price))
它成功了。谢谢大家的帮助 定义方法时,要命名参数:
def fishstore(fish, price):
调用该方法时,引用的是两个不存在的变量:
fishstore(fish, price)
你可能是说:
fishstore(fishname(), number())
fishname()
的结果在该fishstore
方法的上下文中是fish
,同样地,number()
变成price
。在该特定上下文之外,这些变量不存在。由于程序中未定义变量名fish和price,因此出现此错误
print(fishstore(fish, price))
我认为您正在尝试从用户读取值并将其传递给fishstore()
这对你有帮助
版本1
直接从fishstore()函数读取输入
def fishname():
user_input=input("Enter Name: ")
return (user_input.title())
def number():
number_input=input("Enter Price: ")
return number_input
def fishstore():
fish_entry = fishname()
price_entry = number()
output = "Fish Type: " + fish_entry + ", costs $" + price_entry
return output
print(fishstore())
版本2
这两个版本都将提供您所期望的结果
output:
Enter Name: tilapia
Enter Price: 10
Fish Type: Tilapia, costs $10
想想看。您在哪里定义
fish
和price
的值?好吧,您没有定义fish
或price
,那么问题是什么?谢谢大家!我想我误读了我想解决的练习题。非常感谢你的帮助!
output:
Enter Name: tilapia
Enter Price: 10
Fish Type: Tilapia, costs $10