- 设置一个范围1-100的随机整数变量,通过while循环,配合input语句,判断输入的数字是否等于随机数
- 无限次机会,直到猜中为止
- 每一次猜不中,会提示大了或小了
- 猜完数字后,提示猜了几次
点击查看完整代码
Python
- import random
- a = random.randrange(100)
- print(a)
- b=int(input("Enter a number: "))
- i=1
- while a!=b:
- if a>b:
- print("小了")
- else:
- print("大了")
- b=int(input("Enter a number: "))
- i=i+1
- else:
- print(i,"次")
本文作者为wzzyhg,转载请注明。