47 python python-3.x
新的python和编程我怎么会得到这个错误?
def cat_n_times(s, n):
while s != 0:
print(n)
s = s - 1
text = input("What would you like the computer to repeat back to you: ")
num = input("How many times: ")
cat_n_times(num, text)
Run Code Online (Sandbox Code Playgroud)
Mik*_*ham 52
失败的原因是因为(Python 3)input返回一个字符串.要将其转换为整数,请使用int(some_string).
您通常不会在Python中手动跟踪索引.实现这样一个功能的更好方法是
def cat_n_times(s, n):
for i in range(n):
print(s)
text = input("What would you like the computer to repeat back to you: ")
num = int(input("How many times: ")) # Convert to an int immediately.
cat_n_times(text, num)
Run Code Online (Sandbox Code Playgroud)我稍微改变了你的API.在我看来n应该是次数,s应该是字符串.
| 归档时间: |
|
| 查看次数: |
307253 次 |
| 最近记录: |