为了账号安全,请及时绑定邮箱和手机立即绑定

运行程序老是提示 while invaild syntax?

运行程序老是提示 while invaild syntax?

慕斯王 2022-05-19 15:15:57
import randomx=random.randint(0,100)input_num=int(raw_input('please enter a number: ')while 0<=input_num<=100:if x>input_num:print 'high'elif input_num>x:print 'low'else:print 'win'breakinput_num=int(raw_input('please enter a number: '))else:print 'you are out if range'运行程序老是提示while 0<=input_num<=100:SyntaxError: invaild syntax
查看完整描述

2 回答

?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

您好,1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”)
该错误将发生在类似如下代码中:
if spam == 42
print('Hello!')

2)使用 = 而不是 ==(导致“SyntaxError: invalid syntax”)
= 是赋值操作符而 == 是等于比较操作。该错误发生在如下代码中:
if spam = 42:

print('Hello!')
3)错误的使用缩进量。(导致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”)
记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。该错误发生在如下代码中:
print('Hello!')
print('Howdy!')
或者:
if spam == 42:
print('Hello!')
print('Howdy!')
或者:
if spam == 42:
print('Hello!')
4)在 for 循环语句中忘记调用 len() (导致“TypeError: 'list' object cannot be interpreted as an integer”)
通常你想要通过索引来迭代一个list或者string的元素,这需要调用 range() 函数。要记得返回len 值而不是返回这个列表。
该错误发生在如下代码中:
spam = ['cat', 'dog', 'mouse']
for i in range(spam):
print(spam[i])


查看完整回答
反对 回复 2022-05-23
?
湖上湖

TA贡献2003条经验 获得超2个赞

while 0=<input_num<=100:

查看完整回答
反对 回复 2022-05-23
  • 2 回答
  • 0 关注
  • 246 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号