【发布时间】:2020-07-22 19:07:35
【问题描述】:
嗨,我刚开始学习 python,这太令人兴奋了!
我正在读这本书: https://automatetheboringstuff.com
如果我按照说明进行操作,一切都会顺利进行,但我尝试自己修改内容,但有时似乎找不到逻辑。
第 2 章有这个练习:
name = ''
while not name:
print('Enter your name:')
name = input()
print('How many guests will you have?')
numOfGuests = int(input())
if numOfGuests:
print('Be sure to have enough room for all your guests.')
print('Done')
它工作正常,但是如果 numOfGuests 不是数字,我会收到此错误:
ValueError: invalid literal for int() with base 10: ''
我的逻辑想法是,我还可以使用 while 命令将该块包含在循环中,并告诉程序如果 numOfGuests != int 它应该继续并返回到 print('你有多少客人?') 部分。
有人可以帮助我了解如何使其工作吗?
谢谢!
【问题讨论】:
-
查看异常处理。
int(...)提出了错误,你抓住它并继续。 docs.python.org/3/tutorial/errors.html#exceptions。您已经遇到了 python 编程的一个重要部分,它很可能会在 Automate The Boring Stuff 中介绍。我的建议是先列出这个问题,然后继续你已经在使用的教程。等你了解了更多的 python 之后再回来。
标签: python automated-tests mu