自学phython,菜鸟一枚,真心求教,不胜感激

xiaoqiangha 2016-01-27 11:34:02
写了一个这样的phython语句:
import random
secret=random.randint(1,10)
print("----------**************-------------")
teep = input("猜一猜")
guess = int (teep)
while guess!=secret :
guess = int (teep)
if guess>secret:
print('biger')
else:
print("smaller")
teep = input("猜错了")
print ("congratulation")
print("end")
执行的结构如下:
猜一猜9
biger
猜错了8
biger
猜错了7
smaller
猜错了8
congratulation
end

实在不明白为什么第一次出现8的时候不直接结束循环,而非要第二次呢?

...全文
447 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
panghuhu250 2016-01-29
  • 打赏
  • 举报
回复
引用 2 楼 panghuhu250 的回复:

if guess>secret:
       print('biger')
    else:
       print("smaller")
分类不全, 大于, 小于 或 等于
想错了.
panghuhu250 2016-01-29
  • 打赏
  • 举报
回复

if guess>secret:
       print('biger')
    else:
       print("smaller")
分类不全, 大于, 小于 或 等于
frogsice 2016-01-29
  • 打赏
  • 举报
回复
import random
secret=random.randint(1,10)
print("----------**************-------------")
teep = input("猜一猜")
guess = int (teep)
while guess!=secret :
    if guess>secret:
       print('biger')
    else:
       print("smaller")
    teep = input("猜错了")
    guess=int(teep)
print ("congratulation")
print("end")
这样就行了吧,你while里比较的是guess,最后赋值给了teep然后就去跟secret比较了,所以会晚一个循环比较 我也是新手就这么一个问题我能回答的出来了哈哈哈哈
scl83036295 2016-01-29
  • 打赏
  • 举报
回复
c请将赋值:guess = int (teep) 放到 teep = input("猜错了") 后即可。
hldh214 2016-01-29
  • 打赏
  • 举报
回复
楼主是从PHP过来的吗
江南 2016-01-28
  • 打赏
  • 举报
回复
看到了就给你回复一下吧 import random secret=random.randint(1,10) print("----------**************-------------") teep = input("猜一猜") guess = int (teep) while guess!=secret :     guess = int (teep)     if guess>secret:        print('biger')     else:        print("smaller")     guess = int(input("猜错了")) print ("congratulation") print("end")

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧