kristin2303 发表于 2020-9-30 21:33:20

新手小白求助代码问题

题目是:1)Write a program that asks the user their first name, last name,and age. 2)Then, save this information in a dictionary and print their full name plus how many years they have until retirement (65 years old).

我写的代码是:
first_name = input("what is your first name"/n)
what is your first name?
Kristin
print(first_name)
last_name = input("what is your last name"/n)
what is your last name?
Koo
print(last_name)
age = input("How old are you"/n)
How old are you?
print(age)
print("first_name"+"last_name")
remaining = (65-age)
print(remaining)

求教:1)这段代码怎样改才正确?总是显示name error,非常感谢
2)题目中要求将用户的姓,名和年龄存到dictionary里面,这个如何在代码中提现?

疾风怪盗 发表于 2020-9-30 21:39:44

# 题目是:1)Write a program that asks the user their first name, last name,and age.
# 2)Then, save this information in a dictionary and print their full name plus how many years they have until retirement (65 years old).

dictionary = {}
first_name = input("what is your first name")# input后面不需要换行符
last_name = input("what is your last name")
dictionary = last_name# 字典形式存储
age = input("How old are you")
print(dictionary)
remaining = (65 - int(age))# input接收的数据是字符串,不能直接运算,要转换为数值
print(first_name + last_name + ',you have ' + str(remaining) + ' years until retirement')# 变量不需要加引号,加了就变成字符串了,不是变量了

昨非 发表于 2020-9-30 21:48:08

first_name = input("what is your first name?")
last_name = input("what is your last name?")
age = int(input("How old are you?"))

name = first_name+last_name
print(name,"'s age is:",age)
print("It's ",65-age," years before retirement")

昨非 发表于 2020-9-30 21:48:42

疾风怪盗 发表于 2020-9-30 21:39


刚刚没刷新,晚了几分钟{:10_245:}

疾风怪盗 发表于 2020-9-30 21:51:10

昨非 发表于 2020-9-30 21:48
刚刚没刷新,晚了几分钟

{:10_312:}

kristin2303 发表于 2020-9-30 22:21:01

疾风怪盗 发表于 2020-9-30 21:39


不好意思,我想问一下,就是这个代码,为啥一开始要弄一个空的dictionary。谢谢🙏

疾风怪盗 发表于 2020-9-30 22:29:24

kristin2303 发表于 2020-9-30 22:21
不好意思,我想问一下,就是这个代码,为啥一开始要弄一个空的dictionary。谢谢🙏

为了后面增加数据啊,你要基础先学完才行啊
如果没有先定义个空字典
就会报错:
Traceback (most recent call last):
File "D:/python/test/test01.py", line 7, in <module>
    dictionary = last_name# 字典形式存储
NameError: name 'dictionary' is not defined

kristin2303 发表于 2020-9-30 22:39:57

疾风怪盗 发表于 2020-9-30 22:29
为了后面增加数据啊,你要基础先学完才行啊
如果没有先定义个空字典
就会报错:


哇!感谢感谢,非常感谢。明白了(我也想先把基础打好,然而教授貌似没从基础开始交,全靠摸索{:10_277:}

疾风怪盗 发表于 2020-9-30 22:42:41

kristin2303 发表于 2020-9-30 22:39
哇!感谢感谢,非常感谢。明白了(我也想先把基础打好,然而教授貌似没从基础开始交,全靠摸索

教授?你还在上学的么?
看看甲鱼的视频,或者哔哩哔哩上搜下黑马程序员,也会有基础的视频讲解,先学完基础,否则瞎子摸象,全靠猜了

kristin2303 发表于 2020-10-1 01:17:54

疾风怪盗 发表于 2020-9-30 22:42
教授?你还在上学的么?
看看甲鱼的视频,或者哔哩哔哩上搜下黑马程序员,也会有基础的视频讲解,先学完 ...

好嘞,谢谢
页: [1]
查看完整版本: 新手小白求助代码问题