因此,我正在编写一段代码,我被分配了作业。我在代码的其他第二部分遇到麻烦。当输入为奇数时,它将转到代码的这一部分,并且在循环重新启动时不会中断。我知道这部分存在缺陷,想知道是否有人可以帮助我看到它。 (很抱歉,如果这段代码看起来非常业余,我正在学习。
我尝试更改输入的位置,但无济于事。
def part1c():
def get_int(prompt):
while True:
try:
return int(butt(prompt))
except ValueError:
pass
def get_ch(prompt):
while True:
res = butt(prompt).strip()
if res:
return res[:1]
def make_row(w , edge, center):
return edge*(w>0) + center*(w-2) + edge*(w>1)
import time
def print_rectangle(h, w, c):
top_row = make_row(w, c, c)
mid_row = make_row(w, c, ' ')
rows = [top_row]*(h>0) + [mid_row]*(h-2) + [top_row]*(h>1)
print('\n'.join(rows))
while True:
print_rectangle(h, w, c)
time.sleep(2)
def main():
h = get_int('Size of box in rows? ')
w = get_int('Size of box in columns? ')
c = get_ch('Character to use: ')
print_rectangle(h, w, c)
while True:
print('Draw boxes:(Enter 0 to stop)')
butt=int(input('If box has an odd number(s), type it.' +
' If not, type the even number(s):') )
if butt == 0:
print('Thank you, next')
break
else:
if butt % 2== 0:
pass
rows=int(input('Size of box in rows? '))
cols= int(input('Size of box in columns? '))
for r in range(rows):
for c in range(cols):
print('*', end='')
print()
else:
butt_is_zero = True
while butt_is_zero:
import sys
# Python 2/3 compatibility shims
if sys.hexversion >= 0x3000000:
butt = input
rng = range
else:
butt = raw_input
rng = xrange
if __name__=="__main__":
main()
if butt==1:
butt_is_zero = False
get_int()
get_ch()
make_row()
print_rectangle()
part1c()
part1c()
答案 0 :(得分:0)
您可以尝试的一件事就是改变
else:
while True:
import sys
else:
butt_is_zero = True
while butt_is_zero:
import sys
然后中断while循环
if butt==1:
butt_is_zero = False
此外,^^上方的消息也没有在循环内定义函数,而是将其置于循环外并根据需要调用它们。
ex
import time
def print_rectangle(h, w, c):
top_row = make_row(w, c, c)
mid_row = make_row(w, c, ' ')
rows = [top_row]*(h>0) + [mid_row]*(h-2) + [top_row]*(h>1)
print('\n'.join(rows))
while True:
print_rectangle(h, w, c)
time.sleep(2)