题目1
使用以下变量
product_code=“377B”
product_name=“牛肉汤”
product_size=“250mL”
product_price=2.15
使用字符串加法编写一个print语句,以便生成以下精确输出:
377B:牛肉汤,250mL
代码
product_code = "377B"
product_name = "牛肉汤"
product_size = "250mL"
product_price = 2.15
print(product_code + ": " + product_name + ", " + product_size)
实验结果
题目2
使用以下变量
product_code=“377B”
product_name=“牛肉高汤”
product_size=“250mL”
product_price=2.15
使用字符串加法编写一个print语句,以便生成以下精确输出:
“牛肉高汤”,250mL
代码
product_name = "\"牛肉高汤\""
product_size = "250mL"
print(product_name + ", " + product_size)
实验结果
这里考察了一个输入带双引号的方法
题目3
使用以下变量
product_code=“377B”
product_name=“牛肉汤”
product_size=“250mL”
product_price=2.15
使用字符串加法编写一个print语句,以便生成以下精确输出:
牛肉高汤,250毫升,2.15美元
代码
product_name = "牛肉汤"
product_size = "250mL"
product_price = "$2.15"
print(product_name + ", " + product_size + ", " + product_price)
实验结果
题目4
这是海洋世界的门票价格:
6岁以下儿童:免费
6至17:7美元
成人:20美元
仔细研究下面的例子,编写一个程序,要求用户预订机票并显示收据。您的程序应该与以下示例完全一样工作
代码
print("Welcome to Ocean World.")
s_children = input("How many tickets for children under 6? ")
children = input("How many tickets for children age between 6-17? ")
adults = input("How many tickets for adults? ")
s_children_num = int(s_children)
children_num = int(children)
adults_num = int(adults)
sum = s_children_num + children_num + adults_num
sum_s = str(sum)
Total_cost = s_children_num * 0 + children_num * 7 + adults_num * 20
Total_cost_s = str(Total_cost)
print("Receipt:" + "\n" + "Number of tickets: " + sum_s + "\n" + "Total cost " + "$" + Total_cost_s)
实验结果
这个实验主要练习类型转换
题目5
在某个游戏中,玩家的目标之一是收集名为“grassie”的代币,为他们的农场购买动物。一头牛的成本是30棵草,一只鸭子的成本是5棵草,而一只小鸡的成本是3棵草。仔细研究下面的例子,并编写一个与例子完全相同的程序:
输入要购买的奶牛数量:1
输入要购买的鸭子数量:3
输入要购买的鸡肉数量:4
成本:
1头牛=30棵草
3只鸭子=15棵草
4只小鸡=12棵草
总计=57颗草
代码
cow = input("Enter number of cows to purchase: ")
duck = input("Enter number of ducks to purchase: ")
chicken = input("Enter number of chicken to purchase: ")
cow_i = int(cow)
duck_i = int(duck)
chicken_i = int(chicken)
cow_s = str(cow)
duck_s = str(duck)
chicken_s = str(chicken)
g_cow = cow_i * 30
g_cow_s = str(g_cow)
g_duck = duck_i * 5
g_duck_s = str(g_duck)
g_chicken = chicken_i * 3
g_chicken_s = str(g_chicken)
sum = cow_i * 30 + duck_i * 5 + chicken_i * 3
sum_s = str(sum)
print("Cost:" + "\n"
+ cow_s + " cow = " + g_cow_s + " grassies" + "\n"
+ duck_s + " duck = " + g_duck_s + " grassies" + "\n"
+ chicken_s + " chick = " + g_chicken_s + " grassies" + "\n"
+ "Total = " + sum_s + " grassies")
实验结果
题目总结
🧨本次实验主要考察大家的输入输出当中的类型转换,建议大家把这题目自己动手练习一下,这些都是我的笔记,供大家参考复习,如果喜欢这个系列的可以关注一下,后期会有更高阶的练习与讲解