我有以下代码:
d
当我运行它时,我得到:
name=input("What do you want to call your file?")
print(name)
name=name+".txt"
print(name)
我想得到:
<whatever was input>
.txt
我尝试过:
<whatever was input>
<whatever was input>.txt
和
input("What do you want to call your file?")+".txt"
但没有运气。
奇怪的是,当我在shell中运行它时,它运行l,并且它在python 3.1中工作,但不是3.4。
答案 0 :(得分:3)
删除结尾处的换行符:
name = input("Q?").rstrip('\n') + "txt"
或者这样做:
# thanks to Delirious Lettuce: f'{input("Filename?").rstrip('\n')}.txt'.replace("\n","")
name = f'{input("Filename?")}.txt'.replace("\n","")
print(name)
尽管如此,input()通常会删除关闭其输入的返回值。
我想知道为什么你得到一个......对我来说name = f'{input("Filename?")}.txt'
作品,如果我输入'tata'并点击返回,name
就会出现'tata.txt'
。