我试图提出一个问题,并让系统存储该问题的答案,然后用Python打印出来

我试图提出一个问题,并让系统存储该问题的答案,然后用Python打印出来,python,Python,我试图询问并回答“你多大了?”这个问题,然后存储用户对该问题的回答并打印出答案。以下是我迄今为止所做的工作: age = input("How old are you?") print('How old are you?', word) agea = input("sixty") 我不能把它储存起来,然后打印成一个字。我更希望不仅这样做,而且能够存储和打印年龄作为一个数字。到目前为止,系统仅打印答案: How old are you? 请尽您所能提供帮助。因此,只需为您添加一些关于代

我试图询问并回答“你多大了?”这个问题,然后存储用户对该问题的回答并打印出答案。以下是我迄今为止所做的工作:

age = input("How old are you?")
print('How old are you?', word)
agea = input("sixty")
我不能把它储存起来,然后打印成一个字。我更希望不仅这样做,而且能够存储和打印年龄作为一个数字。到目前为止,系统仅打印答案:

    How old are you?

请尽您所能提供帮助。

因此,只需为您添加一些关于代码的解释:

age = input("How old are you?") # Read the input from the user and save it to the 'age' variable

print('How old are you?', word) # Print message, using 'word' as a variable. 
# This should throw an error message since 'word' is not assigned anywhere above.

agea = input("sixty") # Print the 'sixty' message, read the user input and save it into 'agea' variable. 
# What I imagine is that you have typing error here. Do you mean 'age' instead of 'agea'?

纠正建议

age = input("How old are you?") # Print message and wait for input from the user,
# assigning it to 'age' variable

print(f"You are {age} years old") # Print the message replacing {age} with the variable content.

在python中,像
word
这样的变量类型没有任何意义。数字是整数或小数,无论大小。这条规则有一些例外,但这是一般情况。 现在,回答您的问题-函数
input()
还可以将您的字符串打印到屏幕上,以便用户知道他必须输入自己的输入

要获得用户的年龄并将其打印出来,您需要做的是:

age = input("How old are you?")
print("you are ", age, " years old")
这将打印用户的年龄。如果要将输入转换为
int
,只需编写

age = int(input("How old are you?"))
但是要注意这一点,因为如果用户不输入整数,就会出现异常。为了避免这种情况,请执行以下操作:

age = input("How old are you?")
if age.isdigit():
    print("you are ", age, " years old")
    age = int(age)
else:
    print("you must input a number!)

将变量设置为输入时,变量的值将成为用户在“您多大岁数?”语句中输入的值。在这种情况下,如果有人在提示符下输入“18”,那么
age
将等于
18
,并且它将打印
您已经18岁。

age=input(“你多大了?”)
打印(“您是“+年龄+”岁。”)
如果您希望用户输入一个字符串,如“六十”,并且结果类似于
您已经六十岁了。
,则可以使输入值只能接受如下字符串值:

age=input(“你多大了?”)
如果age.isdigit():
打印(“无效”)
其他:
打印(“您是“+年龄+”岁。”)
#或#打印(f“你{age}岁”)

您可以使用字典,如下所示:

num2words = {1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10: 'Ten', 
             11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15: 'Fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen', 
            19: 'Nineteen', 20: 'Twenty', 30: 'Thirty', 40: 'Forty', 50: 'Fifty', 60: 'Sixty', 70: 'Seventy', 80: 'Eighty', 
            90: 'Ninety', 0: 'Zero'}



def n2w(n):
        try:
            print (num2words[n])
        except KeyError:
            try:
                print (num2words[n-n%10] + " " + num2words[n%10].lower())
            except KeyError:
                print ('Number out of range')



if __name__ == "__main__":

    a = input("Type you age using numbers:")
    n2w(int(a))

在本例中,我选择数字作为整数,并将其转换为整数。输入和输出为:

Type you age using numbers:55
Fifty five

如果程序运行正常,你能给每一行举个例子吗?我不能理解你想做什么,你能补充一些说明吗?而且变量
word
没有定义。根据第一条语句,您的答案存储在变量
age
中。然后您尝试打印一个未定义的变量
word
,而您应该打印
age