欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python输错重输while小程序

程序员文章站 2024-03-18 17:49:58
...

age_of_oldboy=98
count=0
while True: 
	count+=1 
	guess_age = int(input("guess age:"))
	 if guess_age==age_of_oldboy: 
		print("You got it !") 
		break 
	elif guess_age<age_of_oldboy:
		print("More bigger!,guess again!") 
	else: 
		print("More smaller!,guess again!") 
	if count == 3: 
		break
print("done!")

或者较好的方法,其中最后一个else对应while条件都不满足时
age_of_oldboy=98
count=0
while count<3: #while True: 
	count+=1 
	guess_age = int(input("guess age:"))
	if guess_age==age_of_oldboy: 
		print("You got it !") 
		break 
	elif guess_age<age_of_oldboy: 
		print("More bigger!,guess again!") 
	else:
		 print("More smaller!,guess again!")
	else: 
		print("you have tried too many times,fuck off!")
另一种方法
age_of_oldboy=98
count=0
while True:
    guess_age = int(input("guess age:"))
    if guess_age==age_of_oldboy:
        print("You got it !")
        break
    elif guess_age<age_of_oldboy:
        print("More bigger!,guess again!")
    else:
        print("More smaller!,guess again!")
    count += 1
    if count==3:
        continue_confirm=input("Do you want keep guessing....?")
        if continue_confirm!=('n'or'N'):
            count=0
        else:
            break
print("game over!")


FOR循环简单笔记
for i in range(0,10,2):#range(10),range(0,10,1),range(0,10,3) 
	print("loop:",i)