Python函数错误
我知道这并不难,但我不断地遇到一个未定义的错误或不同的错误,我尝试了我能想到的一切来得到解决方案。我将输入变量放在代码之外,它部分工作。我第一次上计算机科学课才3周左右。谢谢你的帮助Python函数错误,python,Python,我知道这并不难,但我不断地遇到一个未定义的错误或不同的错误,我尝试了我能想到的一切来得到解决方案。我将输入变量放在代码之外,它部分工作。我第一次上计算机科学课才3周左右。谢谢你的帮助 # function that prompts the user for a name and returns it def user(): name = input("Please enter your name: ") return name # function that receives
# function that prompts the user for a name and returns it
def user():
name = input("Please enter your name: ")
return name
# function that receives the user's name as a parameter, and prompts the user for an age and returns it
def userAge(name):
age = input("How old are you, {}? ".format(name))
return age
# function that receives the user's name and age as parameters and displays the final output
def finalOutput(name, age):
age2x = int(age) * 2
print("Hi, {}. You are {} years old. Twice your age is {}.").format(name, age, str(age2x))
###############################################
# MAIN PART OF THE PROGRAM
# implement the main part of your program below
# comments have been added to assist you
###############################################
# get the user's name
user()
# get the user's age
userAge("name")
# display the final output
finalOutput("name", "age")
您没有在此处存储用户提供的值,也没有将它们传递回函数调用:
user()
userAge("name")
finalOutput("name", "age")
将上述行更改为:
name = user()
age = userAge(name)
finalOutput(name,age)
更正1:
不要传递带有双引号的参数,这意味着您传递的是字符串文本,而不是变量的实际值。
例如,如果将变量名指定为“Jhon”,并将其作为userAge(“name”)传递给函数,则表示将字符串文字“name”传递给userAge(),而不是变量值“Jhon”
输出:名称
def printName(name):
print(name)
name = "jhon"
printName(name)
输出:jhon
最好将返回值分配给一些Valerie,并在不使用双引号的情况下通过@TBurgis
更正2:
print语句中的语法错误。应该使用正确的语法
print("Hi, {}. You are {} years old. Twice your age is {}.".format(name, age, str(age2x)))
如果您有错误,则包括这些错误的完整堆栈跟踪,而且您没有捕获函数的返回值,因此它们会消失在以太中。您有什么问题?欢迎使用堆栈溢出!我们需要更多的信息来帮助您:到目前为止,您尝试了哪些修复,具体出了什么问题?看,和。
print("Hi, {}. You are {} years old. Twice your age is {}.".format(name, age, str(age2x)))