for eachemployee in Employeelist:
name = str(input("Could you tell me what is your name: "))
print ("Hello!"+name)
income = int(input("May I ask your monthly income: "))
我想让输入提示在询问收入时说出人名,例如:
如果我在hui
时输入"Could you tell me what is your name: "
,我想要问下一个问题"May I ask hui's monthly income: "
。
我该怎么做?
答案 0 :(得分:1)
修改强>
实际上,最好这样做:
income = int(input("May I ask {}{} monthly income: "
.format(name, "'" if name.endswith('s') else "'s")))
通过使用conditional expression和str.endswith
,我们确保不会在已经以's
结尾的名称末尾意外添加s
。
您只需将名称插入带有str.format
的提示字符串:
income = int(input("May I ask {}'s monthly income: ".format(name)))
此外,由于str(input(...))
始终返回字符串对象,因此没有理由input
。
最后,您应该使用,
与print
代替+
:
print("Hello!", name)
否则,输出将是Hello!hui
,这是不可读的。
答案 1 :(得分:1)
for eachemployee in Employeelist:
name = input("Could you tell me what is your name: ") # str unnecessary
print ("Hello! " + name)
income = int(input("May I ask {0}'s monthly income:".format(name)))
答案 2 :(得分:0)
替代方式:
income = int(input("May I ask " + name + "'s your monthly income: "))