Python 添加递归并将for循环转换为while循环

Python 添加递归并将for循环转换为while循环,python,Python,这是我的代码,我正在尝试添加递归,因此当用户输入一个小于0的数字时,它不仅会说输入无效,还会显示原始提示,让他们尝试另一个数字。我还试图将for循环更改为while循环。有什么帮助吗 space = '\t' star = '*' size = int(input("Enter the height of the pattern (must be greater than 0): ")) if size <= 0 : print("Invalid E

这是我的代码,我正在尝试添加递归,因此当用户输入一个小于0的数字时,它不仅会说输入无效,还会显示原始提示,让他们尝试另一个数字。我还试图将for循环更改为while循环。有什么帮助吗

space = '\t'
star = '*'
size = int(input("Enter the height of the pattern (must be greater than 0): "))

if size <= 0 :
    print("Invalid Entry!\n")

for i in range(0, size) :
    star_count = 2 * i - 1

    line = space * (size - i - 1)

    if i == 0 :
        line += "1"
    else :
        line += str(2 * i) + space

    line += (star + space) * star_count

    if i > 0 :
        line += str(2 * i + 1)

    print(line)

如果大小不大于0,则可以添加条件以重复输入循环

space = '\t'
star = '*'

size = -1
while size <= 0:
    size = int(input("Enter the height of the pattern (must be greater than 0): "))
    if size <= 0 :
        print("Invalid Entry!\n")

for i in range(0, size) :
    star_count = 2 * i - 1

    line = space * (size - i - 1)

    if i == 0 :
        line += "1"
    else :
        line += str(2 * i) + space

    line += (star + space) * star_count

    if i > 0 :
        line += str(2 * i + 1)

    print(line)

for循环有什么问题

space = '\t'
star = '*'
while 1:
    size = int(input("Enter the height of the pattern (must be greater than 0): "))
    if size > 0:
        break
    print("Invalid Entry!")
    print("Please Try again.")

i = 0
while i < size:
    star_count = 2 * i - 1

    line = space * (size - i - 1)

    if i == 0 :
        line += "1"
    else :
        line += str(2 * i) + space

    line += (star + space) * star_count

    if i > 0 :
        line += str(2 * i + 1)
    i += 1

    print(line)
space='\t'
星='*'
而1:
size=int(输入(“输入图案高度(必须大于0):”)
如果大小>0:
打破
打印(“无效条目!”)
打印(“请重试”)
i=0
而我的尺寸:
星数=2*i-1
行=空间*(大小-i-1)
如果i==0:
行+=“1”
其他:
行+=str(2*i)+空格
行+=(星形+空格)*星形计数
如果i>0:
直线+=str(2*i+1)
i+=1
打印(行)

此代码的限制是,您始终必须输入3才能获得根据您的查询想要得到的答案

space = '\t'
star = '*'
size = int(input("Enter the height of the pattern (must be greater than 0): "))

while size <= 0 :
    print("Invalid Entry!\n")
    size = int(input("Enter the height of the pattern (must be greater than 0): "))

for i in range(0, size) :
    star_count = 2 * i - 1

    line = space * (size - i - 1)
    if i == 0 :
        line += "1"
    else :
        line += str(2 * i) + space
    line += (star + space) * star_count
    if i > 0 :
        line += str(2 * i + 1)
    print(line)
space='\t'
星='*'
size=int(输入(“输入图案高度(必须大于0):”)
当大小为0时:
直线+=str(2*i+1)
打印(行)

您可能希望在循环中检查用户输入,而不是递归。如果用户提供的值大于0,则循环将终止。您的代码永远不会按预期显示输出。对您的问题的部分解决方案是在行下更改大小,以确定这是否回答了您的问题。和来自的代码。
space = '\t'
star = '*'
while 1:
    size = int(input("Enter the height of the pattern (must be greater than 0): "))
    if size > 0:
        break
    print("Invalid Entry!")
    print("Please Try again.")

i = 0
while i < size:
    star_count = 2 * i - 1

    line = space * (size - i - 1)

    if i == 0 :
        line += "1"
    else :
        line += str(2 * i) + space

    line += (star + space) * star_count

    if i > 0 :
        line += str(2 * i + 1)
    i += 1

    print(line)
space = '\t'
star = '*'
size = int(input("Enter the height of the pattern (must be greater than 0): "))

while size <= 0 :
    print("Invalid Entry!\n")
    size = int(input("Enter the height of the pattern (must be greater than 0): "))

for i in range(0, size) :
    star_count = 2 * i - 1

    line = space * (size - i - 1)
    if i == 0 :
        line += "1"
    else :
        line += str(2 * i) + space
    line += (star + space) * star_count
    if i > 0 :
        line += str(2 * i + 1)
    print(line)