|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目是: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里面,这个如何在代码中提现?
- # 题目是: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[first_name] = 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') # 变量不需要加引号,加了就变成字符串了,不是变量了
复制代码
|
|