Error
- 1.SyntaxError:Missing parentheses in call to ‘print’
parentheses -括弧
此bug的原因是版本不同造成,如下图所示:
第一个为2.x版本print的语法
第二个为3.x版本print的语法
- 2.TypeError: unorderable types: str() > int()
此bug的原因是因为:python3中,input函数返回的是字符串,
代码中存在字符串与数字比较的情况
解决如下图:把str强制转换成int
代码:
import random
secret = random.randint(1, 100)
guess = 0
tries = 0
print ("ahoy!it's a number from 1-99.i'll give you 6 tries!")
while guess != secret and tries < 6:
guess = int(input("what's your guess?"))
if guess < secret:
print ("too low!")
elif guess > secret:
print ("too high!!")
tries = tries + 1
if guess == secret:
print ("you got it!!")
else:
print ("game over ! good luck next time~!")
print ("the secret number is", secret)
- 3.ValueError: could not convert string to float: ‘julia’
消息表示python无法从‘julia’创建一个数。
这属于类型转换错误,如果向int(),float()提供的不是一个数,就不会正常显示。
- 4.NameError: name ‘raw_input’ is not defined
python2.x中使用raw_input()从用户处得到一个字符串;
python3.x中用input()代替。
>>> somename = raw_input()
Traceback (most recent call last):
File "" , line 1, in
somename = raw_input()
NameError: name 'raw_input' is not defined
>>> somename = input()
julia
>>> print("hello " + somename + " good afternoon!")
hello julia good afternoon!
>>>