有什么不对这个输入code?在python和浏览器启动 [英] What is wrong with this input code?? and browser launcher in python

查看:148
本文介绍了有什么不对这个输入code?在python和浏览器启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code ........................................ ...........当它运行时,它只能运行redditNeed == 1和它打开5个窗口。然后当我打任何其他除了主页它给我的WebBrowser别人没有定义

  redditNeed =输入(你想达到这版(Subreddit);页面{1}前面,athesism {2},比特币{3},政治{4},世界新闻{5} )    如果redditNeed == 1:
        进口web浏览器
        URL =htt​​p://reddit.com
        webbrowser.open(URL)    如果redditNeed == 2:
        进口web浏览器
        URL =htt​​p://reddit.com/r/atheism
        webbrowser.open(URL)    如果redditNeed == 3:
        进口web浏览器
        URL =htt​​p://reddit.com/r/bitcoin
        webbrowser.open(URL)    如果redditNeed == 4:
        进口web浏览器
        URL =htt​​p://www.reddit.com/r/politics
        webbrowser.open(URL)    如果redditNeed == 5:
        进口web浏览器
        URL =htt​​p://www.reddit.com/r/worldnews
        webbrowser.open(URL)


解决方案

一个几件事情:


  1. 您应该使用 的raw_input ;

  2. 您应该只进口web浏览器一次,在脚本的顶部;

  3. 而不是分支 ELIF S,使用字典来保存选项;和

  4. 您可以使用 webbrowser.open 参数尝试打开一个新的标签。

把它们一起:

 进口web浏览器reddits = {1:{名:头版,URL:http://reddit.com},...}打印(你要哪一个reddit的?)
在排序(reddits)R:
    打印({0}。{1 [名]}。格式(R,reddits [R])
reddit_need = INT(的raw_input(输入一个数字:))webbrowser.open(reddits [reddit_need] [URL],2)

this is the code ...................................................when it runs it will only run the redditNeed == 1 and it opens 5 windows. And then when i hit any other the others besides main page it gives me webbrowser is not defined

    redditNeed = input("which subreddit do you want to reach; front page{1}, athesism{2}, bitcoin{3}, politics{4}, worldnews{5}")

    if redditNeed == 1:
        import webbrowser
        url="http://reddit.com"
        webbrowser.open(url)

    if redditNeed == 2:
        import webbrowser
        url="http://reddit.com/r/atheism"
        webbrowser.open(url)

    if redditNeed == 3:
        import webbrowser
        url="http://reddit.com/r/bitcoin"
        webbrowser.open(url)

    if redditNeed == 4:
        import webbrowser
        url="http://www.reddit.com/r/politics"
        webbrowser.open(url)

    if redditNeed == 5:
        import webbrowser
        url="http://www.reddit.com/r/worldnews"
        webbrowser.open(url)

解决方案

A couple of things:

  1. You should use raw_input;
  2. You should only import webbrowser once, at the top of the script;
  3. Rather than branching elifs, use a dictionary to hold the options; and
  4. You can use webbrowser.open's new argument to try to open a new tab.

Putting that together:

import webbrowser

reddits = {1: {"name": "Front Page", "url": "http://reddit.com"}, ...}

print("Which Reddit do you want?")
for r in sorted(reddits):
    print("{0}. {1[name]}".format(r, reddits[r])
reddit_need = int(raw_input("Enter a number: "))

webbrowser.open(reddits[reddit_need]["url"], 2)

这篇关于有什么不对这个输入code?在python和浏览器启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆