这种情况只适用于初学者,老司机请绕行。
[En]
This case is only suitable for beginners, old drivers please make a detour.
1、案例介绍:
程序本身有一个数字,用户输入一个数字,两个数字进行比较。
[En]
The program itself has a number, the user enters a number, two numbers are compared.
2、应用到的知识点:
input函数、字符串、while循环、if条件判断语句、break语句。
3、全部代码
my_num = 8
如果我要猜三次 N次 直到猜对位置
Python学习交流群:924040232
times = 1
while True:
print(f'开始第 {times} 次猜数字')
you_num = input("请输入一个数字(1-10):")
# 如果输入的内容不是一个数字
if you_num.isdigit():
you_num = int(you_num)
if my_num == you_num:
print(f'恭喜你猜对了,当前的数字为{my_num}')
break # 猜对了才会进入第 22 行
# elif my_num > you_num:
else: # 猜错了才会进入的 23 行
# print(f'你太菜了,这都能猜错, 当前的数字为{my_num}, 你输入的数字是{you_num}')
# print('你太笨啦,这都能猜错')
if my_num > you_num:
print('数字猜小了')
else:
print('数字猜大了')
if times == 3:
print(f'你已经没有机会了')
break
times += 1
else:
print('输入错误,请输入一个数字')
对于那些了解基础知识的人来说,这很容易。如果您有任何问题,欢迎留言。你自己去试试吧。
[En]
It’s easy for those who know the basics. If you have any questions, you are welcome to leave a message. Go and try it yourself.
Original: https://www.cnblogs.com/hahaa/p/15965937.html
Author: 轻松学Python
Title: 【Python入门级案例】猜数字小游戏-猜N次
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509882/
转载文章受原作者版权保护。转载请注明原作者出处!