我在使用以下代码时遇到问题:
if verb == "stoke":
if items["furnace"] >= 1:
print("going to stoke the furnace")
if items["coal"] >= 1:
print("successful!")
temperature += 250
print("the furnace is now " + (temperature) + "degrees!")
^this line is where the issue is occuring
else:
print("you can't")
else:
print("you have nothing to stoke")
Run Code Online (Sandbox Code Playgroud)
产生的错误如下:
Traceback(most recent call last):
File "C:\Users\User\Documents\Python\smelting game 0.3.1 build
incomplete.py"
, line 227, in <module>
print("the furnace is now " + (temperature) + "degrees!")
TypeError: must be str, not int
Run Code Online (Sandbox Code Playgroud)
我不确定问题是什么,因为我已经将名称从温度更改为温度并在温度附近添加括号但仍然发生错误.
>>> def main():
fahrenheit = eval(input("Enter the value for F: "))
celsius = fahrenheit - 32 * 5/9
print("The value from Fahrenheit to Celsius is " + celsius)
>>> main()
Enter the value for F: 32
Traceback (most recent call last):
File "<pyshell#73>", line 1, in <module>
main()
File "<pyshell#72>", line 4, in main
print("The value from Fahrenheit to Celsius is " + celsius)
TypeError: Can't convert 'float' object to str implicitly"
Run Code Online (Sandbox Code Playgroud) 我是编码的新手,所以我决定使用unicode为测试目的制作某种密码,我已经通过在Unicode中添加数字来做到这一点,所以这有点秘密.我一直在收到这个错误,但我不知道如何解决它.有什么解决方案吗?这是代码:
while True:
try:
message = int(input("Enter a message you want to be decrypt: "))
break
except ValueError:
print("Error, it must be an integer")
secret_string = ""
for char in message:
secret_string += chr(ord(char - str(742146))
print("Decrypted", secret_string)
q = input("")
Run Code Online (Sandbox Code Playgroud) 我正在尝试组合下面的列表,以“dd/hh:mm”格式显示日期。
名单如下:
dd = [23, 23, 24, 24, 24, 24, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27]
hh = [21, 23, 7, 9, 16, 19, 2, 5, 12, 15, 22, 1, 8, 11, 18, 21, 2, 8, 12, 12, 13, 13, 18, 22]
mm = [18, 39, 3, 42, 52, 43, 46, 41, 42, 35, 41, 27, 37, 30, 0, 58, 57, 51, 11, 20, 18, 30, 35, 5] …Run Code Online (Sandbox Code Playgroud) 尝试将count int添加到字符串的末尾(网站URL):
码:
count = 0
while count < 20:
Url = "http://www.ihiphopmusic.com/music/page/"
Url = (Url) + (count)
#Url = Url.append(count)
print Url
Run Code Online (Sandbox Code Playgroud)
我想要:
http://www.ihiphopmusic.com/music/page/2
http://www.ihiphopmusic.com/music/page/3
http://www.ihiphopmusic.com/music/page/4
http://www.ihiphopmusic.com/music/page/5
Run Code Online (Sandbox Code Playgroud)
结果:
Traceback (most recent call last):
File "grub.py", line 7, in <module>
Url = Url + (count)
TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud) 我读了其他的问题,但我试图做的事情是不同的我试图在python中制作一个计算器,并试图将变量输入事物变成一个整数,所以我可以添加它.这是我的代码还没有完成,我是一个初学者:
print("Hello! Whats your name?")
myName = input()
print("What do you want me to do? " + myName)
print("I can add, subtract, multiply and divide.")
option = input('I want you to ')
if option == 'add':
print('Enter a number.')
firstNumber = input()
firstNumber = int(firstNumber)
print('Enter another number.')
secondNumber = input()
secondNumber = int(secondNumber)
answer = firstNumber + secondNumber
print('The answer is ' + answer)
Run Code Online (Sandbox Code Playgroud)
它能做什么:
Hello! Whats your name?
Jason
What do you want me to do? Jason
I …Run Code Online (Sandbox Code Playgroud) 我是Python的新手,所以我一直在运行自己的练习,只是开始记忆基本的功能和语法.我正在使用Pycharm IDE和Python 3.4.在运行一些基本的str/int连接练习时,我遇到了一个问题.下面的每个实例都抛出一个不受支持的操作数类型.Stackoverflow上有几个线程清楚地说明了正确的连接语法,但上面的错误信息继续困扰着我.
print ("Type string: ") + str(123)
print ("Concatenate strings and ints "), 10
Run Code Online (Sandbox Code Playgroud) def main():
total = 0.0
totalcom = 0.0
name = input("Please enter your name: ")
for x in range(1, 8):
sales = float(input("Please enter your sales from day", x))
total += sales
commission = sales * .1
totalcom += commission
print("Your total sales is: ", total)
print("Your commission is: ", totalcom)
main()
Run Code Online (Sandbox Code Playgroud)
我的目标本质上是佣金计算器.我应该从用户那里获得每天的销售额.但是,我希望用户知道他们输入的信息是在哪一天.我得到的错误是"输入预期最多一个参数,得到2".那么有没有办法在输入语句中使用x?
def myFunc(King):
print("The characters name is " + King.name + " and age is " + King.age)
Run Code Online (Sandbox Code Playgroud)
如何将 int 链接到字符串?
在Python中,我们可以做到:
'a' * 4
Run Code Online (Sandbox Code Playgroud)
得到'aaaa'.
我们做不到:
'a' + 4
Run Code Online (Sandbox Code Playgroud)
我们首先必须将4转换为字符串.
这只是一个任意选择,使用String和Int arg的定义重载*,并且不为String和Int重载+?
我试图理解这是如何适应Python被认为是'强类型',如果我们将其定义为不执行任何隐式类型转换的语言,例如它在上面的例子中没有将4转换为字符串.那么在第一个例子中,这是运算符重载的一个例子,而不是隐式转换?
在文档中,我找不到mul或*的定义,它采用Int和String,只有2个Int的形式.我们如何知道存在重载定义?
我想打印数字和一些字符串,例如:
print("root: " + rootLeaf + " left:" + leftLeaf + "sum: " +(rootLeaf+leftLeaf) )
Run Code Online (Sandbox Code Playgroud)
这里“root”,“left”和“sum”是字符串,其中rootLeaf和leftleaf是整数,我想找到它们的总和。
我在这里检查了帖子,但无法实现整数之和(字符串连接中的数学运算)
我正在尝试对一组列执行循环,如下所示:
for i in range(len(parent)):
for j in range(4):
table.cell(i, j).text = str(locals()["child_" + i + "_" + j])
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
TypeError: can only concatenate str (not "int") to str
Run Code Online (Sandbox Code Playgroud)
线路错误 table.cell(i, j).text = str(locals()["child_" + i + "_" + j])
让我们以这段代码为例:
Women = (input("What's the number of women?"))
Men = (input("What's the number of men?"))
print("Percentage of Men: " + ((Men//(Men+Women))*100) + "\n Percentage of Women: " + ((Women//(Men+Women))*100))
Run Code Online (Sandbox Code Playgroud)
我收到一个错误Class 'str' does not define '__floordiv__', so the '//' operator cannot be used on its instances。
我该如何解决这个问题?