找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2659|回复: 2

[求助] 各位大神,帮忙看看,我的小代码运行不顺畅 :-(

0

主题

0

帖子

0

积分

贫民

积分
0
Unique 发表于 2021-3-23 18:32:48 | 显示全部楼层 |阅读模式
本帖最后由 Unique 于 2021-3-23 18:46 编辑

代码如下:----------
def print_box(width, height, mark):
    for n in range(1,height + 1):
        print(mark * width)

def read_input(string):
    user_input = True
    while user_input:
        if int(input(string)) > 0:
            user_input = False
    return int(input(string))

def main():
    width = read_input("Enter the width of a frame: ")
    height = read_input("Enter the height of a frame: ")
    mark = input("Enter a print mark: ")
    print()
    print_box(width, height, mark)

if __name__ == "__main__":
    main()
目前方程和格式我不想做变动,期待结果:

Enter the width of a frame: -1
Enter the width of a frame: 0
Enter the width of a frame: -4
Enter the width of a frame: 4
Enter the height of a frame: -1
Enter the height of a frame: 0
Enter the height of a frame: 2
Enter a print mark: #


####
####

我的结果:
Enter the width of a frame: -1
Enter the width of a frame: 2
Enter the width of a frame: 3
Enter the height of a frame: 0
Enter the height of a frame: 8
Enter the height of a frame: 4
Enter a print mark: #


###
###
###
###

请帮忙看看,问题出在哪儿?为什么我的代码只读取第二个正数,而非第一个出现的正数?如何改进呢?非常感谢!!!




回复

使用道具 举报

0

主题

18

帖子

18

积分

贫民

积分
18
宫崎峻Miyazaki 发表于 2021-7-30 22:16:15 | 显示全部楼层
从23行开始,就全部报错
  1. Enter the width of a frame: -1
  2. Enter the width of a frame: 0
  3. Enter the width of a frame: -4
  4. Enter the width of a frame: 4
  5. Enter the height of a frame: -1
  6. Enter the height of a frame: 0
  7. Enter the height of a frame: 2
  8. Enter a print mark: #


  9. ####
  10. ####

  11. 我的结果:
  12. Enter the width of a frame: -1
  13. Enter the width of a frame: 2
  14. Enter the width of a frame: 3
  15. Enter the height of a frame: 0
  16. Enter the height of a frame: 8
  17. Enter the height of a frame: 4
  18. Enter a print mark: #

复制代码
回复 支持 反对

使用道具 举报

0

主题

102

帖子

102

积分

侠客

积分
102
傻眼貓咪 发表于 2021-8-3 23:44:23 | 显示全部楼层

  1. # 樓主,你的問題不清晰,最好是連題目也發布。你這題我看過,題目大概是想列印出一個圖採用的寬,高度和圖案三個參數隨自己輸入

  2. # 對於你的read_input(string)最後回傳了int(input(string)),也就代表回傳數據在最後又被更新了
  3. # 必須清楚知道你的函數是要做什麼?是否有回傳值?,回傳什麼值?才能開始寫函數
  4. # 以下是我的代碼,你參考看看:

  5. def read_value():
  6.     while True:
  7.         try:
  8.             w = int(input('enter the width of a frame: '))
  9.             if w <= 0: continue
  10.             break
  11.         except ValueError: continue
  12.     while True:
  13.         try:
  14.             h = int(input('enter the height of a frame: '))
  15.             if h <= 0: continue
  16.             break
  17.         except ValueError: continue
  18.     p = input('enter a print pattern: ')
  19.     return [w, h, p]

  20. def draw_rectangle(x: '[width, height, pattern]') -> 'draw a rectangle':
  21.     print()
  22.     for i in range(x[1]):
  23.         print(x[2]*x[0])

  24. def main():
  25.     draw_rectangle(read_value())

  26. if __name__ == '__main__':
  27.     main()
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表