请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册
首页 社区 Python python中错误,但不知道错在哪

python中错误,但不知道错在哪

猿梦 2023-1-2 18:26:29
正确代码如下:import&nbsp;randomsecret&nbsp;=&nbsp;random.randint(1,&nbsp;100)guess&nbsp;=&nbsp;0tries&nbsp;=&nbsp;0print&nbsp;&quot;AHOY!&nbsp;&nbsp;I'm&nbsp;the&nbsp;Dread&nbspirate&nbsp;Roberts,&nbsp;and&nbsp;I&nbsp;have&nbsp;a&nbsp;secret!&quot;print&nbsp;&quot;It&nbsp;is&nbsp;a&nbsp;number&nbsp;from&nbsp;1&nbsp;to&nbsp;99.&nbsp;&nbsp;I'll&nbsp;give&nbsp;you&nbsp;6&nbsp;tries.&quot;while&nbsp;guess&nbsp;!=&nbsp;secret&nbsp;and&nbsp;tries&nbsp;<&nbsp;6:&nbsp;&nbsp;&nbsp;&nbsp;guess&nbsp;=&nbsp;input(&quot;what's&nbsp;yer&nbsp;guess?&nbsp;&quot;)&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;guess&nbsp;<&nbsp;secret:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&quot;Too&nbsp;low,&nbsp;ye&nbsp;scurvy&nbsp;dog!&quot;&nbsp;&nbsp;&nbsp;&nbsp;elif&nbsp;guess&nbsp;>&nbsp;secret:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&quot;Too&nbsp;high,&nbsp;landlubber!&quot;&nbsp;&nbsp;&nbsp;&nbsp;tries&nbsp;=&nbsp;tries&nbsp;+&nbsp;1if&nbsp;guess&nbsp;==&nbsp;secret:&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&quot;Avast!&nbsp;Ye&nbsp;got&nbsp;it!&nbsp;&nbsp;Found&nbsp;my&nbsp;secret,&nbsp;ye&nbsp;did!&quot;else:&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&quot;NO&nbsp;more&nbsp;guesses!&nbsp;&nbsp;Better&nbsp;luck&nbsp;next&nbsp;time&nbsp;matey1&quot;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&quot;The&nbsp;secret&nbsp;number&nbsp;was&quot;,&nbsp;secret&nbsp;执行结果及你的错误原因如图:
本大人龙水彤踢坏~开关他透%比起教程的里面少了$ python那是linux下的运行方式。你在windows下在IDEL中这些语句都是在linux里敲的,如果要用windows遍写python就不应该用你现在看
贫道谢易蓉透'椅子谢依风拿出来$先把代码拷贝到python自带的IDE试一试,是不是有些字符不对!而且,楼主应该贴出错误的信息,好让人做判断。如果是编码的问题,可以在文件的第一行(一定是第一行),加上:# -*- encoding:utf-8 -*-
老子她们踢坏了足球&贫僧小春煮熟,1,注意符号,asc码的大于小于等于符号和GBK的大于小于等于符号不一样2,看不懂的尽量自己加上注释,跳过不懂的,再通篇对照注释理解我给修改了一下,加上注释,试着理解吧。--------------#-*-coding:utf-8import randomsecret = random.randint(1, 100)print secrettries = 0print &quot;AHOY!  I'm the Dread Pirate Roberts, and I have a secret!&quot;print &quot;It is a number from 1 to 99.  I'll give you 6 tries.&quot;while 1:guessStr = raw_input(&quot;what's yer guess? \n&quot;)try:guess=int(guessStr)except:#不是数字就返回print &quot;please give a number!&quot;continuetries+=1 #输入即用掉一次机会if guess < secret:print &quot;Too low, ye scurvy dog!&quot;elif guess > secret:print &quot;Too high, landlubber!&quot;else:#第一次小于,第二次大于,最后判断等于,如果输入对的就直接跳出print &quot;Avast! Ye got it!  Found my secret, ye did!&quot;breakif tries<6:#小于6次有得输入,第6次还出错就跳出continueelse:print &quot;NO more guesses!  Better luck next time matey1&quot;print &quot;The secret number was %s&quot;%secretbreak
本大人方以冬取回‘寡人方寻绿撞翻*你的小于号用的是全角 <显然不行,要改成半角的<,在程序语句里是不能用全角字符的。

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册