如何多次显示一个整数
How to display an integer many times
我想创建一个函数,可以根据需要将整数加 2。它看起来像这样:
>>> n = 3
>>> add_two(n)
Would you like to add a two to n ? Yes
The new n is 5
Would you like to add a two to n ? Yes
the new n is 7
Would you like to add a two to n ? No
有人可以帮我吗?我不知道如何在不回忆函数的情况下打印句子。
您可以在 add_two()
函数中使用循环。所以,你的函数可以在不调用函数的情况下打印句子。
您可以将其包装在一个函数中。一旦不满足“是”条件,函数就会中断
def addd(n):
while n:
inp = input('would like to add 2 to n:' )
if inp.lower() == 'yes':
n = n + 2
print(f'The new n is {n}')
else:
return
addd(10)
我们的想法是在您的函数中使用一个 while
循环,每次您让它继续添加两个。否则,它退出。
鉴于这些知识,我建议您先自己尝试一下,但我会在下面提供一个解决方案,您可以将自己的解决方案与之进行比较。
该解决方案可以简单到:
while input("Would you like to add a two to n ?") == "Yes":
n += 2
print(f"the new n is {n}")
但是,由于我很少错过改进代码的机会,因此我也会提供一个更复杂的解决方案,但有以下区别:
- 它先打印起始编号;
- 它允许添加任意数字,如果提供none则默认为两个;
- 输出文本稍微更人性化;
- 它需要一个是或否的答案(实际上任何以大写或小写
y
或 n
开头的都可以,其他所有内容都将被忽略并重新提出问题)。
def add_two(number, delta = 2):
print(f"The initial number is {number}")
# Loop forever, relying on break to finish adding.
while True:
# Ensure responses are yes or no only (first letter, any case).
response = ""
while response not in ["y", "n"]:
response = input(f"Would you like to add {delta} to the number? ")[:1].lower()
# Finish up if 'no' selected.
if response == "n":
break
# Otherwise, add value, print it, and continue.
number += delta
print(f"The new number is {number}")
# Incredibly basic/deficient test harness :-)
add_two(2)
上面的答案详细描述了做什么和为什么,如果您正在寻找满足您要求的非常简单的初学者型代码,试试这个:
n = 3
while True:
inp = input("Would you like to add 2 to n? Enter 'yes'/'no'. To exit, type 'end' ")
if inp == "yes":
n = n + 2
elif inp == "no":
None
elif inp == "end": # if the user wants to exit the loop
break
else:
print("Error in input") # simple input error handling
print("The new n is: ", n)
我想创建一个函数,可以根据需要将整数加 2。它看起来像这样:
>>> n = 3
>>> add_two(n)
Would you like to add a two to n ? Yes
The new n is 5
Would you like to add a two to n ? Yes
the new n is 7
Would you like to add a two to n ? No
有人可以帮我吗?我不知道如何在不回忆函数的情况下打印句子。
您可以在 add_two()
函数中使用循环。所以,你的函数可以在不调用函数的情况下打印句子。
您可以将其包装在一个函数中。一旦不满足“是”条件,函数就会中断
def addd(n):
while n:
inp = input('would like to add 2 to n:' )
if inp.lower() == 'yes':
n = n + 2
print(f'The new n is {n}')
else:
return
addd(10)
我们的想法是在您的函数中使用一个 while
循环,每次您让它继续添加两个。否则,它退出。
鉴于这些知识,我建议您先自己尝试一下,但我会在下面提供一个解决方案,您可以将自己的解决方案与之进行比较。
该解决方案可以简单到:
while input("Would you like to add a two to n ?") == "Yes":
n += 2
print(f"the new n is {n}")
但是,由于我很少错过改进代码的机会,因此我也会提供一个更复杂的解决方案,但有以下区别:
- 它先打印起始编号;
- 它允许添加任意数字,如果提供none则默认为两个;
- 输出文本稍微更人性化;
- 它需要一个是或否的答案(实际上任何以大写或小写
y
或n
开头的都可以,其他所有内容都将被忽略并重新提出问题)。
def add_two(number, delta = 2):
print(f"The initial number is {number}")
# Loop forever, relying on break to finish adding.
while True:
# Ensure responses are yes or no only (first letter, any case).
response = ""
while response not in ["y", "n"]:
response = input(f"Would you like to add {delta} to the number? ")[:1].lower()
# Finish up if 'no' selected.
if response == "n":
break
# Otherwise, add value, print it, and continue.
number += delta
print(f"The new number is {number}")
# Incredibly basic/deficient test harness :-)
add_two(2)
上面的答案详细描述了做什么和为什么,如果您正在寻找满足您要求的非常简单的初学者型代码,试试这个:
n = 3
while True:
inp = input("Would you like to add 2 to n? Enter 'yes'/'no'. To exit, type 'end' ")
if inp == "yes":
n = n + 2
elif inp == "no":
None
elif inp == "end": # if the user wants to exit the loop
break
else:
print("Error in input") # simple input error handling
print("The new n is: ", n)