|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 华文杉 于 2021-10-10 15:31 编辑
这是原始代码:- def cheese_and_crackers(cheese_count, boxes_of_crackers):
- print(f"You have {cheese_count} cheeses!")
- print(f"You have {boxes_of_crackers} boxes of crackers!")
- print("Man that's enough for a party!")
- print("Get a blanket.\n")
- print("We can just give the function numbers directly:")# 注意这一行,我们也是首先对这一行的内容进行打印
- cheese_and_crackers(20, 30)# 定义两个变量的值
- print("OR, we can use variables from our script:")# 然后是这里
- amount_of_cheese = 10
- amount_of_crackers = 50#分别修改两个变量的值
- cheese_and_crackers(amount_of_cheese, amount_of_crackers)#修改两个变量的值
- print("We can even do math inside too:")# 现在是第三个自然段。
- cheese_and_crackers(10 + 20, 5 + 6)# 修改两个变量的内容
- print("And we can combine the two, variables and math:")# 打印内容
- cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)
复制代码
这是仿写代码:
- def A1_and_B2(after_one, before_two):
- print(f"你有{after_one}根铅笔。")
- print(f"你有{before_two}张纸。")
- print("这足够写一些东西了!\n")
- print("首先,我们可以这样做:")
- A1_and_B2(10, 20)
- print("或者,我们可以让你来试一试:")
- A1 = input("请输入铅笔的数量:")
- B2 = input("请输入纸张的数量:")
- print("现在,我们还有一种方法,但是要对上面的量进行重定义,定义中...")
- A1_and_B2(A1, B2)
- print("现在我们可以进行再次的重定义了:")
- A1_and_B2(5 + 6, 8 + 9)
- print("现在,还有一种加法:")
- A1_and_B2(A1 + 20, B2 + 50)
复制代码
这是错误显示:
- Traceback (most recent call last):
- File "ex19P1.py", line 24, in <module>
- A1_and_B2(A1 + 20, B2 + 50)
- TypeError: must be str, not int
复制代码
请问为什么会出现问题?我想让用户输入A_1and_B2(10, 20)里面的10和20我应该怎么编写代码呢?
- def A1_and_B2(after_one, before_two):
- print(f"你有{after_one}根铅笔。")
- print(f"你有{before_two}张纸。")
- print("这足够写一些东西了!\n")
- print("首先,我们可以这样做:")
- A1_and_B2(10, 20)
- print("或者,我们可以让你来试一试:")
- A1 = int(input("请输入铅笔的数量:"))
- B2 = int(input("请输入纸张的数量:"))
- print("现在,我们还有一种方法,但是要对上面的量进行重定义,定义中...")
- A1_and_B2(A1, B2)
- print("现在我们可以进行再次的重定义了:")
- A1_and_B2(5 + 6, 8 + 9)
- print("现在,还有一种加法:")
- A1_and_B2(A1 + 20, B2 + 50)
复制代码
|
|