原程序:
import random
secret = random.randint(1,99)
guess=0
tries=0
print "AHOY! im the dread print Roberts,and i have a secret!"
s a nunber form 1 to 99.i
print "itll give you 6 tries"
s yer guess?")
while guess != secret and tries < 6:
guess =input("what
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! i
m the dread print Roberts,and i have a secret!
s a nunber form 1 to 99.i
itll give you 6 tries
s yer guess?50
what
too low,ye scurvy dog!
whats yer guess?90
s yer guess?80
too high,landlubber!
what
too high,landlubber!
whats yer guess?70
s yer guess?60
too high,landlubber!
what
too low,ye scurvy dog!
whats yer guess?75
s yer guess?70
too high,landlubber!
what
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次呢?
你是guess > secret时才会+1,如果 too low输入 100次也不会结束,就是明白?就是 不要把tries = tries + 1和前面那一行并列,和下面一行if 并列,因为你把tries = tries + 1放入到了elif中
你放代码的时候直接放代码框里,不然排版出问题,其他语言还好,python的缩进影响程序的运行,你这里运行了8次应该是把
tries = tries + 1
放到了elif里面,应该每次猜都要+1,与elif并列
下面我放的python3的代码,print有区别
import random
secret = random.randint(1,99)
guess=0
tries=0
print ("AHOY! im the dread print Roberts,and i have a secret!")
print ("its a nunber form 1 to 99.ill give you 6 tries")
while guess != secret and tries < 6:
guess = int(input("whats 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)
mport random
secret = random.randint(1,100)
guess = 0
tries = 0
print "AHOY I'm the Dread Pirate Roberts, and I have a secret!"
print "It is a number from 1 to 99. I will give you 6 tries. "
while guess !=secret and tries < 6 :
guess = input("what is yer guess? ")
if guess < secret:
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 !"
elif tries >= 6:
print "No more gusses ! Better luck next time ,matey!"
print "The secret number was",secret
python 3的代码看这里:https://blog.csdn.net/a245049656/article/details/79206148
我就不写了。