python-麻烦将用户输入转换为整数格式

时间:2017-08-15 23:34:34

标签: python python-3.x

user_input = ("Enter a number and I will tell you the sum of the multiples of 3 and 5 leading up to it.")
number = int(user_input)

我只是想从用户那里获取输入并对其进行某些测试。例如,如果数字> 0:。为了做到这一点,我假设用户输入数据自动存储为字符串并为了解决这个问题,人们可以做代码框中提供的内容。但是,在执行此操作时,我得到ValueError:对于带有基数为10的int()的无效文字:'输入一个数字,我将告诉您3和5的倍数之和,并指出它。&#39 ;显然我做错了。有人能说清楚如何正确地做到这一点吗?

- 提前感谢你:)

1 个答案:

答案 0 :(得分:3)

您实际上并没有提示输入数字。您示例中的from bs4 import BeautifulSoup import urllib.request import re def getLinks(url): html_page = urllib.request.urlopen(url) soup = BeautifulSoup(html_page, "html.parser") links = [] for link in soup.findAll('a', attrs={'href': re.compile("^http://")}): links.append(link.get('href')) return links anchors = getLinks("http://madisonmemorial.org/") for anchor in anchors: happens = urllib.request.urlopen(anchor) if happens.getcode() == "404": # Do stuff # Click on links and return responses countMe = len(anchors) for anchor in anchors: i = getLinks(anchor) countMe += len(i) happens = urllib.request.urlopen(i) if happens.getcode() == "404": # Do some stuff print(countMe) 是字符串文字user_input,无法将其转换为数字。

可以使用Enter a number and I will tell you the sum of the multiples of 3 and 5 leading up to it.提示用户输入。

input()