Demo26
**
**
Enter weight and price for package 1: 50, 24.59 LErter
Enter weight and price for package 2: 25,11.99 P Ente
Package 2 has the better price.
程序编辑:
# 数据:重量,价钱
# 步骤:
# 1.请输入第一种所购产品的重量和价格
# 2.请输入第二种所购产品的重量和价格
# 3,进行比较,输出结果。
a1, a2 = eval(input("请输入第一种所购产品的重量和价格:") )
b1, b2 = eval(input("请输入第二种所购产品的重量和价格:") )
if (a2 / a1) > (b2 /b1):
print("第一种产品和第二种产品相比之下,第二种产品更好")
elif (a2 / a1) == (b2 /b1):
print("第一种产品和第二种产品相比之下,两种产品一样好")
elif (a2 / a1) < (b2 /b1):
print("第一种产品和第二种产品相比之下,第一种产品更好")
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/26.py
请输入第一种所购产品的重量和价格:50,24.59
请输入第二种所购产品的重量和价格:25,11.99
第一种产品和第二种产品相比之下,第二种产品更好
Process finished with exit code 0
Demo27
**
**
Enter an integer: 10
Is 10 divisibie by 5 and 6? False
Is 10 divisible by 5 or 6? True
Is 10 divisible by 5 or 6, but not both? True
程序编辑:
# 数据:一个数字
# 步骤:
# 1.请输入一个整数
# 2.检测该数字是否能被5和6都整除、 能被5或6整除还是只被它们
# 中的一个整除(但又不能被它们同时整除)。
# 3,输出结果。
a = int(input("请输入一个整数:") )
if a % 5 == 0 and a % 6 == 0:
print("%d这个数能被5和6都整除" % a)
elif a % 5 != 0 and (a % 6 == 0):
print("%d这个数能被6整除,但不能被5整除" % a)
elif (a % 5 == 0) and (a % 6 != 0):
print("%d这个数能被5整除,但不能被6整除" % a)
elif (a % 5 != 0) and (a % 6 != 0):
print("%d这个数不能被5或者6整除" % a)
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/27.py
请输入一个整数:20
20这个数能被5整除,但不能被6整除
Process finished with exit code 0
Demo28
**
**
scissor (0),rock (1), paper (2): 1
The computer is scissor. You are rock. You won.
scissor (0), rock (1), paper (2): 2
The computer is paper. You are paper too. It is a draw.
程序编辑:
"""
数据:电脑随机的 com 用户输入的 usr
步骤:
1.提示用户输入0 1 2
2.电脑随机产生0 1 2
3.对比
石头
1
剪刀 布
0 2
0 - 1 = -1
1 - 2 = -1
2 - 0 = 2
"""
import random
com = random.randint(0, 2)
usr = int(input("剪刀(0),石头(1),布(2):"))
com_str = ""
usr_str = ""
if com == 0:
com_str = "剪刀"
elif com == 1:
com_str = "石头"
else:
com_str = "布"
if usr == 0:
usr_str = "剪刀"
elif usr == 1:
usr_str = "石头"
else:
usr_str = "布"
if com == usr:
print("玩家是%s,电脑是%s,是一个平局" % (usr_str, com_str))
elif usr - com == -1 or usr - com == 2:
print("玩家是%s,电脑是%s,玩家输" % (usr_str, com_str))
else:
print("玩家是%s,电脑是%s,玩家赢" % (usr_str, com_str))
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/28.py
剪刀(0),石头(1),布(2):3
玩家是布,电脑是石头,玩家输
Process finished with exit code 0
Demo29
**
**
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 0
Enter the dollar amount: 100
$100.0 is 681.0 yuan
Enter the exchange rate from dollars to RMB: 6.81 P
Enter 0 to convert dollars to RMB and 1 vice versa: 1 P
Enter the RMB amount: 10000
10000.0 yuan is $1468. 43.
Enter the exchange rate from dollars to RMB: 6.81
Enter 0 to convert dollars to RMB and 1 vice versa: 5
Incorrect input
程序编辑:
# 数据:汇率资金
# 步骤: 1. 提示用户输入美元与人民币之间的汇率
# 2.提示用户输入转换方式
# 3.提示用户输入资金
# 4.计算转换后的资金
# 5.输出结果
rate = eval(input("Enter the exchange rate from do1lars to RMB: "))
choice = eval(input("Enter 0 to convert do11ars to RMB and 1 vice versa: "))
amount = eval(input("Enter the do1lar amount: "))
if choice == 0:
amount1 = amount * rate
print("$%.1f is %.1f yuan"%(amount,amount1))
elif choice == 1:
amount1 = amount / rate
print("%.1f yuan is $%.2f"%(amount, amount1))
else :
print("Incorrect input")
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/29.py
Enter the exchange rate from do1lars to RMB: 6.81
Enter 0 to convert do11ars to RMB and 1 vice versa: 1
Enter the do1lar amount: 10000
10000.0 yuan is $1468.43
Process finished with exit code 0
Demo30
**
**
Enter three edges: 1, 1, 1
The perimeter is 3
Enter three edges: 1, 3,1
The input is invalid
程序编辑:
a, b, c = eval(input("Enter three sides:"))
if a + b > c and a + c > b and b + c > a:
print(a+b+c,"合法")
else :
print("不合法")
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/30.py
Enter three sides:1,3,1
不合法
Process finished with exit code 0