无法在Python中添加这些字符串

时间:2018-01-22 05:28:57

标签: python

我正在尝试在下面添加这些字符串,但我一直收到错误。我尝试了很多东西,他们使用不同的设置在其他文件中工作,但我不能在这里工作,我不知道为什么。问题在于字符串“healthy_kibble”。我希望它是“kibble”+ 100的总和。但我尝试的一切似乎给了我不同的错误。这是两个例子

from sys import argv

script, filename = argv

def pickles_puppy(kibble, chicken):
    print(f"Pickles likes eating {kibble} pieces of kibble and {chicken} pieces of chicken")

kibble = input("how many pieces of kibble does Pickles want?")
chicken = input("and what about chicken?")
infinity = 1000000

pickles_puppy(kibble, chicken)

f = open(filename, 'w')
f.truncate()        

healthy_kibble =  (kibble + 1 + 100)

f.write(f'''Pickles is a little nugget \n
if it was up to her shie would only eat {infinity} pieces of chicken \n
But she has to be healthy \n
So she also has to eat her {healthy_kibble} pieces of kibble''' )
f.close()

2 个答案:

答案 0 :(得分:0)

kibble = int(input("how many pieces of kibble does Pickles want?"))

希望它可以帮到你。

答案 1 :(得分:0)

如果你在尝试操作它之后在粗磨输入之后添加它,你就会知道出了什么问题。

print(type(kibble))

我刚刚发表了这个声明并得到了:

<class 'str'>

正如Pradam所观察到的,如果将输入从string转换为int,您将能够更好地将值添加为整数。

当然,如果你进入,总会有一个迟钝的答案,

healthy_kibble =  (kibble + '1' + '100')

你会得到

的不太有用的答案
331100