「已注销」 2018-10-26 02:40 采纳率: 0%
浏览 2397
已采纳

关于循环次数计算的问题?

原程序:
import random
secret = random.randint(1,99)
guess=0
tries=0
print "AHOY! im the dread print Roberts,and i have a secret!"
print "it
s a nunber form 1 to 99.ill give you 6 tries"
while guess != secret and tries < 6:
guess =input("what
s yer guess?")
if guess print "too low,ye scurvy dog!"
elif guess > secret:
print "too high,landlubber!"

        tries = tries + 1

if guess ==secret:
print "avast!ye got it! found my secret,ye did!"
else:
print "no more guesses!better luck next time,matey!"
print "the secret numaber was",secret

F5之后:
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

================================ RESTART ================================

AHOY! im the dread print Roberts,and i have a secret!
it
s a nunber form 1 to 99.ill give you 6 tries
what
s yer guess?50
too low,ye scurvy dog!
whats yer guess?90
too high,landlubber!
what
s yer guess?80
too high,landlubber!
whats yer guess?70
too high,landlubber!
what
s yer guess?60
too low,ye scurvy dog!
whats yer guess?75
too high,landlubber!
what
s yer guess?70
too high,landlubber!
what`s yer guess?65
too high,landlubber!
no more guesses!better luck next time,matey!
the secret numaber was 62

为什么**tries < 6:** 但是结果却是50,90,80,70,60,75,70,65共计8次呢?

  • 写回答

3条回答

  • xingjianfengaa 2018-10-26 06:19
    关注

    你是guess > secret时才会+1,如果 too low输入 100次也不会结束,就是明白?就是 不要把tries = tries + 1和前面那一行并列,和下面一行if 并列,因为你把tries = tries + 1放入到了elif中

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流