【发布时间】: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之后。