【问题标题】:please fix my python code which is print out function请修复我的打印输出功能的python代码
【发布时间】:2021-11-05 17:49:39
【问题描述】:

我尝试创建打印出最后 2 位数字但打印出星形的代码

这是我的代码,我想打印出形状内的最后 2 位数字

### number 1 shape with star mark ###

def one():
    x_str = "";
    for row in range (0,6):
        for col in range(0,6):
            if(col==3 and row>0) or (row==2 and col==2):
                x_str = x_str + "*"
            else:
                x_str = x_str + " "
        x_str = x_str + "\n"
    return x_str

### number 2 shape with star mark ###
def two():
    x_str = "";
    for row in range (0,6):
        for col in range(0,6):
            if(col==4 and row>1 and row<3) or (col==2 and row>3 and row<5) or (row==1 and col>1 and col<5) or (row==3 and col>1 and col<5) or (row==5 and col>1 and col<5):
                x_str = x_str + "*"
            else:
                x_str = x_str + " "
        x_str = x_str + "\n"
    return x_str

###  number 3 shape with star mark ###
def three():
    x_str = "";
    for row in range (0,6):
        for col in range(0,6):
            if(col==4 and row>1 and row<3) or (col==4 and row>3 and row<5) or (row==1 and col>1 and col<5) or (row==3 and col>1 and col<5) or (row==5 and col>1 and col<5):
                x_str = x_str + "*"
            else:
                x_str = x_str + " "
        x_str = x_str + "\n"
    return x_str

###  number 4 shape with star mark ###
def four():
    x_str = "";
    for row in range (0,6):
        for col in range(0,6):
            if(col==4 and row>0) or (col==2 and row>0 and row<4) or (col==3 and row==3):
                x_str = x_str + "*"
            else:
                x_str = x_str + " "
        x_str = x_str + "\n"
    return x_str

### main code ###
shapes = {'1':one(),'2':two(),'3':three(),'4':four()}
x = words[-2:]

while True:
    words = input("please enter 8digit number between 1 to 4")
    output = "".join([shapes[number] for number in x if number in shapes])
    if output:
        print("Your number is" +words+ ",the last two-digit number is:")
        print(output, end='\r')

    elif words == "QUIT":
        break
    else:
        print("Error: invalid words please try again")

我尝试编写代码,但无法正常工作。

你们可以编辑我的代码以正常工作吗?

非常感谢各位。

【问题讨论】:

  • x = words[-2:] 应该在您分配words之后

标签: python printing


【解决方案1】:

这应该可以工作

### number 1 shape with star mark ###

def one():
    x_str = "";
    for row in range (0,6):
        for col in range(0,6):
            if(col==3 and row>0) or (row==2 and col==2):
                x_str = x_str + "*"
            else:
                x_str = x_str + " "
        x_str = x_str + "\n"
    return x_str

### number 2 shape with star mark ###
def two():
    x_str = "";
    for row in range (0,6):
        for col in range(0,6):
            if(col==4 and row>1 and row<3) or (col==2 and row>3 and row<5) or (row==1 and col>1 and col<5) or (row==3 and col>1 and col<5) or (row==5 and col>1 and col<5):
                x_str = x_str + "*"
            else:
                x_str = x_str + " "
        x_str = x_str + "\n"
    return x_str

###  number 3 shape with star mark ###
def three():
    x_str = "";
    for row in range (0,6):
        for col in range(0,6):
            if(col==4 and row>1 and row<3) or (col==4 and row>3 and row<5) or (row==1 and col>1 and col<5) or (row==3 and col>1 and col<5) or (row==5 and col>1 and col<5):
                x_str = x_str + "*"
            else:
                x_str = x_str + " "
        x_str = x_str + "\n"
    return x_str

###  number 4 shape with star mark ###
def four():
    x_str = ""
    for row in range(0, 6):
        for col in range(0, 6):
            if(col==4 and row> 0) or (col==2 and row>0 and row<4) or (col==3 and row==3):
                x_str = x_str + "*"
            else:
                x_str = x_str + " "
        x_str = x_str + "\n"
    return x_str

### main code ###
shapes = {'1':one(),'2':two(),'3':three(),'4':four()}

while True:
    words = input("please enter 8digit number between 1 to 4")
    output = "".join([shapes[number] for number in words[-2:] if number in shapes])
    if output:
        print("Your number is" +words[-2:]+ ",the last two-digit number is:")
        print(output, end='\r')
    elif words == "QUIT":
        break
    else:
        print("Error: invalid words please try again")

【讨论】:

  • 感谢您的帮助。我还有一个简短的问题。如果我输入“12341234”,将打印出带有形状的 3 和 4,但打印出的是不同的行。我怎样才能在同一行打印。 ex) 当前打印的 3(at line 1) 4(at line2) --> 想要更改为 3 4(at line 1)
  • 如果我帮助了你,请接受我的回答:) 我不知道我也理解你的问题。但也许这可以帮助你:stackoverflow.com/questions/43372078/… .
猜你喜欢
  • 1970-01-01
  • 2023-02-24
  • 2023-03-22
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多