早教吧作业答案频道 -->英语-->
python程序问题(TypeError:cannotconcatenate'str'and'int'objects)原题是要求运行时是这样的:Pleasethinkofanumberbetween0and100!Isyoursecretnumber50?Enter'h'toindicatetheguessistoohigh.Enter'l'toindicatet
题目详情
python程序问题(TypeError:cannot concatenate 'str' and 'int' objects)
原题是要求运行时是这样的:
Please think of a number between 0 and 100!
Is your secret number 50?
Enter 'h' to indicate the guess is too high.Enter 'l' to indicate the guess is too low.Enter 'c' to indicate I guessed correctly.l(这个 l 以及下面的 h 和 c 是自己手动输入的)
Is your secret number 75?
Enter 'h' to indicate the guess is too high.Enter 'l' to indicate the guess is too low.Enter 'c' to indicate I guessed correctly.l
Is your secret number 87?
Enter 'h' to indicate the guess is too high.Enter 'l' to indicate the guess is too low.Enter 'c' to indicate I guessed correctly.c
Game over.Your secret number was:87
然后我的程序如下:
print "Please think of a number between 0 and 100!"
high=100
low=0
p=(low + high) / 2
print "Is your secret number" + p +"?"
i=raw_input("Enter 'h' to indicate the guess is too high.Enter 'l' to indicate the guess is too low.Enter 'c'to indicate I guessed correctly.")
while ='c':
if i=='h':
high = p
elif i=='l':
low = p
else :
print "Sorry,I did not understand your input."
p=(high + low) / 2
print "Is your secret number" + p +"?"
i=raw_input("Enter 'h' to indicate the guess is too high.Enter 'l' to indicate the guess is too low.Enter 'c'to indicate I guessed correctly.")
print "Game over.Your secret number was:"+ p
可是我运行时出了错误:TypeError:cannot concatenate 'str' and 'int' objects
ERROR:Failing on first line.
我不懂,求问我要怎么改呢(T^T)
原题是要求运行时是这样的:
Please think of a number between 0 and 100!
Is your secret number 50?
Enter 'h' to indicate the guess is too high.Enter 'l' to indicate the guess is too low.Enter 'c' to indicate I guessed correctly.l(这个 l 以及下面的 h 和 c 是自己手动输入的)
Is your secret number 75?
Enter 'h' to indicate the guess is too high.Enter 'l' to indicate the guess is too low.Enter 'c' to indicate I guessed correctly.l
Is your secret number 87?
Enter 'h' to indicate the guess is too high.Enter 'l' to indicate the guess is too low.Enter 'c' to indicate I guessed correctly.c
Game over.Your secret number was:87
然后我的程序如下:
print "Please think of a number between 0 and 100!"
high=100
low=0
p=(low + high) / 2
print "Is your secret number" + p +"?"
i=raw_input("Enter 'h' to indicate the guess is too high.Enter 'l' to indicate the guess is too low.Enter 'c'to indicate I guessed correctly.")
while ='c':
if i=='h':
high = p
elif i=='l':
low = p
else :
print "Sorry,I did not understand your input."
p=(high + low) / 2
print "Is your secret number" + p +"?"
i=raw_input("Enter 'h' to indicate the guess is too high.Enter 'l' to indicate the guess is too low.Enter 'c'to indicate I guessed correctly.")
print "Game over.Your secret number was:"+ p
可是我运行时出了错误:TypeError:cannot concatenate 'str' and 'int' objects
ERROR:Failing on first line.
我不懂,求问我要怎么改呢(T^T)
▼优质解答
答案和解析
Python allow to concatenate strings by '+',but here,your p is an integer.
So,to solve it,you can use either of these:
1.print 'Is your secret number " + str(p) + "
2.print 'Is your secret number %d?"%p
(for multiple integers,you can '%d %d %d'%(num1,num2,num3)
3.print 'Is your secret number {0}?".format(p)
I personally like the second one best.It's more of c style,doesn't it?
So,to solve it,you can use either of these:
1.print 'Is your secret number " + str(p) + "
2.print 'Is your secret number %d?"%p
(for multiple integers,you can '%d %d %d'%(num1,num2,num3)
3.print 'Is your secret number {0}?".format(p)
I personally like the second one best.It's more of c style,doesn't it?
看了 python程序问题(Typ...的网友还看了以下:
A.one and a half year`sB.a year and a half`sC.one 2020-04-05 …
一条铁路,修完9000千米后,剩余部分比全长的四分之三少300千米,这条铁路多长?求算式and方程 2020-04-27 …
一块手表的分针长2厘米,他的针尖一昼夜走多少厘米?需要算式and过程。PS: 2020-05-14 …
1-lg2=?麻烦了最好有个说明and过程 2020-05-14 …
A.B=软件工程OR R.C =S.C AND R.D =S.D B.B=‘软件工程‘ORRC =S 2020-05-26 …
数学计算正确and过程SD~!可只能选做,但是后5题必做-1/4+(-2)^2-(-3)^2+(- 2020-06-04 …
数据库VFP计算机二级的一道题目查询选修C2课程号的学生姓名,下列SQL语句中错误的是()。A)S 2020-07-10 …
取9.0g甲物质升温使之汽化测其密度是相同条件下氢气的45倍求甲相对分子质量急求高人and过程详解 2020-07-12 …
python程序问题(TypeError:cannotconcatenate'str'and'in 2020-07-25 …
ARM指令AND和ORR的用法,求详解过程例子。:已知R5=0xF0811880,R6=0x4ARM 2020-12-15 …