我声明了一个空列表并询问用户输入,以便我可以将其附加到列表中。我还保留了一个计数器,让用户在列表中告诉他们想要的最大数量的术语。但是当我运行该程序时,while循环不起作用,比较无用。列表增量未在Python中自动生成3/2
我也尝试过制作跨Python,所以它可以在Python 2,3以上版本中运行。
这里是我的代码
# Asking users for their search terms #
def user_searchterms():
version = (3,0) # for python version 3 and above
cur_version = sys.version_info
if cur_version >= version:
try:
maxterms = input("Enter Maximum Terms By You[EX: 1/2/3] >> ") # User tells the maximum search terms he wants
maxlist = maxterms
while len(search_keyword) < maxlist:
item = input("Enter Your Item >> ")
search_keyword.append(item)
except Exception as e:
print(str(e))
else:
try:
maxterms = raw_input("Enter Maximum Terms By You[EX: 1/2/3] >> ")
maxlist = maxterms
while len(search_keyword) < maxlist:
item = raw_input("Enter Your Items >> ")
search_keyword.append(item)
except Exception as e:
print "ERROR: Exception Occured!"
user_searchterms() # for starting the function
Image to demonstrate the problem
编辑:我在Python2.7测试。 我想让用户输入数字来告诉他们想添加多少搜索词。然后while循环运行一个循环来追加用户输入的搜索词。
这里的想法: 假设用户输入3时询问搜索词的数量,一个变量将保存该值。然后while循环会将该值与数组的长度进行比较。如果数组小于该值(数组中的项小于用户输入的数字),则循环将要求用户输入字符串search_term并将该字符串附加到数组中。
我试图制作一个谷歌图片下载器,可以询问用户的搜索条件。
在此先感谢
请准确地说出您的期望会发生什么,以及发生了什么。 “while循环不起作用,并且比较没有用”,这是相当模糊的。 – Carcigenicate
你让它跨越python,但是你跑的是什么版本? P2? – roganjosh
'input'和'raw_input()'返回字符串值。如果你想把它转换成一个整数,就像你看到的那样,对结果调用int()。 –