超级菜鸟在这里求助一下了!
猜数字游戏的作业(猜1到100中的整数),自己写到了这里
有这么几个问题:
1.只有六次机会,用完6次之后我应该怎么提示用户已经猜完了6次机会呢(我写的这个当中,到了六次就直接结束,尝试过加入提示语,但是老是不正确)?
2.老师叫把提示改成warmer或者是cooler(取代原先的直接提示数字),这个真不知道怎么改
或者哪位还有更好的方案?
非常非常的感谢!!
import random
def special_input(low,high):
prompt = "Enter a number betweet "+str(low)+" and "+str(high)
number_in = input(prompt)
while not number_in.isnumeric() or ( int(number_in) < low ) or ( int(number_in) > high ):
number_in = input(prompt)
return int(number_in)
secret = random.randint(1,100)
print("secret:",secret) #DEBUG
guess = special_input(1,100)
diff = abs(secret - guess)
if guess == secret:
print("Well done - and on the first go, too!")
else:
print("You were out by", diff)
x = 0
while guess != secret and x < 5:
print('Try again ')
guess = special_input(1,100)
diff = abs(secret - guess)
if guess == secret:
print('Well done, you got it (in the end)')
break
if guess != secret:
print('You were out by', diff)
x = x + 1
猜数字游戏的作业(猜1到100中的整数),自己写到了这里
有这么几个问题:
1.只有六次机会,用完6次之后我应该怎么提示用户已经猜完了6次机会呢(我写的这个当中,到了六次就直接结束,尝试过加入提示语,但是老是不正确)?
2.老师叫把提示改成warmer或者是cooler(取代原先的直接提示数字),这个真不知道怎么改
或者哪位还有更好的方案?
非常非常的感谢!!
import random
def special_input(low,high):
prompt = "Enter a number betweet "+str(low)+" and "+str(high)
number_in = input(prompt)
while not number_in.isnumeric() or ( int(number_in) < low ) or ( int(number_in) > high ):
number_in = input(prompt)
return int(number_in)
secret = random.randint(1,100)
print("secret:",secret) #DEBUG
guess = special_input(1,100)
diff = abs(secret - guess)
if guess == secret:
print("Well done - and on the first go, too!")
else:
print("You were out by", diff)
x = 0
while guess != secret and x < 5:
print('Try again ')
guess = special_input(1,100)
diff = abs(secret - guess)
if guess == secret:
print('Well done, you got it (in the end)')
break
if guess != secret:
print('You were out by', diff)
x = x + 1