关于python for循环使用的小问题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sinat_33290369/article/details/88538571

首先说明一下,我在书上遇到了一段带有for的python程序,其中for语句写在一行,我运行了一下,没有问题,但是我自己测试使用for语句,写一行就会出错,所以想问一下,有关for语句的使用,还请各位道友不吝赐教。

源代码

功能:实现在26个英文字母和9个数字中随机生成一个8位密码

# 随机密码生成
chars=['0','1','2','3','4','5','6','7','8','9','a','b','c','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
from random import choice
for i in range(10):
	print(''.join([choice(chars) for i in range(8)]))

运行后的结果

IDLLE编辑器

自己又随便写一个程序

print('a') for i in range(8)

运行后的结果

在这里插入图片描述
所以想问 for 循环语句写成一行有什么要求吗?

展开阅读全文

python小问题关于importerror

12-18

rn在pycharm中写个python程序,包括FBCrawl.py,html_login.py,common/glovar.py,在glovar.py中定义了一个webdriver_browser, 要在FBCrawl.py和html_login.py中使用这个 webdriver_browser,就是一个项目中多个文件使用一个变量,主要功能是登录facebook 抓取网页内容,使用了selenium,运行时报错:说是importError,所以没能打印出html_cont的内容rn运行结果:[img=https://img-bbs.csdn.net/upload/201612/18/1482055230_933938.jpg][/img]rnC:\Python\Python36\python.exe C:/Python/PyCharmProject/FaceBookCrawl/FBCrawl.pyrnNonernException ignored in: >rnTraceback (most recent call last):rn File "C:\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 173, in __del__rn File "C:\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 149, in stoprn File "C:\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 120, in send_remote_shutdown_commandrn ImportError: sys.meta_path is None, Python is likely shutting downrnrn Process finished with exit code 0rnrnrn程序在这里rnrnFBCrwal.py:rnrnfrom common import glovarrnfrom html_login import HtmlLoginrnfrom selenium import webdriverrnrnif __name__ == '__main__':rnrn baseurl = "http://www.google.com"rn #log in the websitern login_url = "https://www.facebook.com/login.php?login_attempt=1&lwv=110"rn HtmlLogin().login(login_url)rn html_cont = glovar.webdriver_browser.get("https://www.facebook.com/alexis.n.griffith.3")rn print(html_cont)rnrnrnhtml_login.py:rnrnfrom selenium import webdriverrnrnfrom common import glovarrnrnrnclass HtmlLogin(object):rnrn def login(self,url):rn if url is None:rn returnrn login_url = urlrn glovar.webdriver_browser = webdriver.Chrome()rn glovar.webdriver_browser.get(login_url)rn #user credentialsrn user = glovar.webdriver_browser.find_element_by_css_selector("#email")rn user.send_keys('********@gmail.com')rn password = glovar.webdriver_browser.find_element_by_css_selector("#pass")rn password.send_keys('********')rn login = glovar.webdriver_browser.find_element_by_css_selector("#loginbutton")rn login.click()rnrnrnglovar.py:rnrnwebdriver_browser = Nonernrn项目路径:rn[img=https://img-bbs.csdn.net/upload/201612/18/1482055230_933938.jpg][/img]rn请各位帮忙看看案问题出在哪里, 论坛

没有更多推荐了,返回首页