Python Practice Exercises: Login, Number Guessing, Grade Classification, Rock‑Paper‑Scissors, and Shopping Cart
This article presents a series of beginner‑level Python exercises—including a login system with limited attempts, a number‑guessing game, grade classification logic, a rock‑paper‑scissors simulator, and a simple shopping‑cart program—complete with full source code and step‑by‑step explanations.
After covering basic Python syntax such as output, strings, loops, conditional statements, tuples, and dictionaries, the author provides several hands‑on practice problems to reinforce learning.
Exercise 1: Login with password – The program asks for a username and a hidden password (using getpass ), allows three attempts, and offers the option to retry after failure.
<code>import getpass
username = "zhangtianci"
password = "1234qwer"
a = 0
while a < 3:
ist_username = input("请输入用户账号:")
ist_password = getpass.getpass("请输入用户密码")
if ist_username == username and ist_password == password:
print("登陆成功,欢迎来到Python编程学习圈")
break
else:
a = a + 1
if a == 1:
print("密码错误,你还有2次机会")
elif a == 2:
print("密码错误,你还有1次机会")
else:
print("你的机会用完了,是否还想再次输入?想再来 输入1, 其他的话按任意键回车退出")
agin = int(input("请输入:"))
if agin == 1:
a = 0
else:
print("再见老弟")
</code>Exercise 2: Number guessing game – The user has three chances to guess a randomly generated number between 1 and 20, receiving feedback after each guess.
<code>import random
a = [1,2,3]
b = 0
num = random.randint(1,20)
print(num)
for i in a:
cc = int(input("请输入你要猜测的数字:(提示:1-20)你只有三次机会"))
if cc == num:
print("恭喜你猜对了")
break
elif cc > num:
b = b + 1
print("你猜大呦")
else:
b = b + 1
print("猜小了呦")
if b == 1:
print("你还有2次输入机会")
elif b == 2:
print("你还有1次输出机会")
else:
print("你没机会了,再见了大兄弟")
</code>Exercise 3: Grade classification – Based on a numeric score, the program prints one of four categories: Excellent, Good, Average, or Pass.
<code>sore = int(input("请输入你的分数:"))
if sore > 90:
print("优秀")
elif sore > 80:
print("不错")
elif sore > 70:
print("一般")
elif sore >= 60:
print("及格")
else:
print("你要努力了")
</code>Exercise 4: Rock‑Paper‑Scissors – The game randomly selects a move, compares it with the user’s input, and reports win, loss, or draw, looping until the user wins.
<code>import random
all_choices = ["石头","剪刀","布"]
a_choices = random.choice(al_choices)
print(a_choices)
a = 0
while a < 999:
quan = input("请出拳 : ")
if quan == "石头":
if quan == all_choices:
print("平局")
elif all_choices == "剪刀":
print("您赢了")
break
else:
print("您输了")
elif quan == "剪刀":
if quan == all_choices:
print("平局")
elif all_choices == "布":
print("您赢了")
break
else:
print("您输了")
else:
if quan == all_choices:
print("平局")
elif all_choices == "石头":
print("您赢了")
break
else:
print("您输了")
</code>Advanced Exercise: Simple Shopping Cart – The script asks the user for their available money, displays a list of helmet products with IDs and prices, lets the user select items by ID, updates the remaining balance, and allows repeated purchases until the user quits with "q".
<code>money = int(input("请输入你手上有多少钱$"))
print("你的金额为",money,"元")
shop = [
("agv头盔",1780),
("MT纤维头盔",1300),
("SHoei仙鹤头盔",3699),
("Arai头盔",4600),
("LS2头盔",379),
("sol头盔",699),
("坦克头盔",350)
]
shoping_cart = []
print("《《《《《《《《《《《《 下面是头盔商品的列表")
for shoping in shop:
print(shop.index(shoping),shoping)
while True:
shop_id = input("请输入你要购买商品的id: 结束购买请按q:")
if shop_id.isdigit():
shop_id = int(shop_id)
if shop_id < len(shop) and shop_id >= 0:
price = shop[shop_id][1]
if money > price:
money = money - price
print("购买成功本次消费金额:",price,"剩余金额",money)
else:
print("你的金额不足,你要购买商品价格为",price,"你的余额为:",money)
else:
print("请输入编号范围之内的数字")
elif shop_id == "q":
print("欢迎下次光临")
break
else:
print("输入有误,请重新输入")
</code>The article concludes with screenshots of the program outputs and a brief note encouraging readers to explore the original source for more details.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.