题目1
编写一个程序,根据以下定价计算成本。
Number of items | Cost |
---|---|
1-50 | 每件3美元 邮费: 10美元 |
超过50 | 每件2美元 邮寄:免费 |
举个例子:
代码
items_num = input("Enter the number of items: ")
items_num_i = int(items_num)
items_num_s = str(items_num)
if 50 >= items_num_i >= 1:
print("\nReceipt:")
items_num_sum = items_num_i * 3
items_num_sum_s = str(items_num_sum)
print(items_num_s + " items" + " x " + "$3" + " = " + "$" + items_num_sum_s)
print("Postage: $10")
Total = items_num_sum + 10
Total_s = str(Total)
print("Total: " + "$" + Total_s)
elif items_num_i > 50:
print("\nReceipt:")
items_num_sum = items_num_i * 2
items_num_sum_s = str(items_num_sum)
print(items_num_s + " items" + " x " + "$2" + " = " + "$" + items_num_sum_s)
print("Postage: $0")
print("Total: " + "$" + items_num_sum_s)
else:
print("please enter number again!")
实验结果
题目2
编写一个程序,根据以下定价计算成本。
Number of items | Cost |
---|---|
1-50 | 每件3美元; 邮寄:默认帖子:10美元 ;注册职位:15美元;特快专递:20美元 |
超过50 | 每件2美元; 邮寄:标准职位:免费;注册职位:10美元;特快专递:17美元 |
举个例子
代码
这次代码的命名用了abc,大家就凑合看一下吧😂
a=input("Enter the number of items: ")
d=input("Enter shipping method (s/r/e): ")
m=int(a)
b=str(a)
if m<=50 and m>=1:
print("\nReceipt:")
s=m*3
ss=str(s)
print(b+" items"+" x "+"$3"+" = "+"$"+ss)
if d=="s":
print("Standard post: $10")
c=s+10
cc=str(c)
print("Total: "+"$"+cc)
elif d=="r":
print("Registered post: $15")
c=s+15
cc=str(c)
print("Total: "+"$"+cc)
elif d=="e":
print("Express post: $20")
c=s+20
cc=str(c)
print("Total: "+"$"+cc)
else:
print("please enter again!")
elif m>50:
print("\nReceipt:")
s=m*2
ss=str(s)
print(b+" items"+" x "+"$2"+" = "+"$"+ss)
if d=="s":
print("Standard post: $0")
print("Total: "+"$"+ss)
elif d=="r":
print("Registered post: $10")
c=s+10
cc=str(c)
print("Total: "+"$"+cc)
elif d=="e":
print("Express post: $17")
c=s+17
cc=str(c)
print("Total: "+"$"+cc)
else:
print("please enter again!")
else:
print("please enter number again!")
实验结果
题目3
编写一个程序,要求用户输入四个整数,然后显示最小数字和最大数字。
举个例子
代码
num1 = int(input("Enter the first integer: "))
num2 = int(input("Enter the second integer: "))
num3 = int(input("Enter the third integer: "))
num4 = int(input("Enter the fourth integer: "))
minimum = min(num1, num2, num3, num4)
maximum = max(num1, num2, num3, num4)
smin=str(minimum)
smax=str(maximum)
print("\nThe minimum number is "+smin+" and the maximum number is "+smax + ".")
实验结果
这里运用到了python的函数min()和max()
题目总结
🥞🥞本次实验主要练习if-elif-else,对于刚开始的学习的同学有一定的实验难度,需要多多练习。如果你也有好的题目可以跟我分享,我们一起讨论与进步吧!