python-error

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 "<pyshell#0>", line 1, in <module>
    somename = raw_input()
NameError: name 'raw_input' is not defined
>>> somename = input()
julia
>>> print("hello " + somename + " good afternoon!")
hello julia good afternoon!
>>> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值