我当前的脚本是为了在设置打开某个网站之前输入3行而设计的。
我的代码名为genius.py
我想知道如何在一行中完成 例如
genius.py eminem lose yourself
我目前的代码是:
#!/usr/bin/python
import webbrowser
artist = raw_input("Enter artist name: ")
song = raw_input("Enter song name: ")
artist = artist.replace(" ", "-")
song = song.replace(" ", "-")
webbrowser.open("http://genius.com/"+artist+"-"+song+"-lyrics")
答案 0 :(得分:1)
命令行参数位于sys.argv
数组中。
import sys
artist = sys.argv[1]
song = sys.argv[2]
您需要引用包含空格的名称:
genius.py "the beatles" "a day in the life"
否则就无法分辨出艺术家的结局和歌曲的开始。
答案 1 :(得分:0)
答案 2 :(得分:-1)
import webbrowser
webbrowser.open('http://genius.com/{0}-{1}-lyrics'.format(raw_input('Enter artist name: ').replace(' ', '_'), raw_input('Enter song name: ').replace(' ', '_')))