Issue
I'm declaring an empty list and asking user for the input so that I can append that into the list. I also kept a counter for the user to tell maximum number of terms they want in a list. But when I run the program, the while loop does not work and comparison is left useless.
I also tried it making cross python, so that it can run in both Python 2,3 and above.
Here's my code
# 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
EDIT: I'm testing in Python2.7. I want to user to enter the number to tell how many search terms they want to add. And then while loop runs a loop to append the search terms that user enters.
Here's the idea: Suppose the user enters 3 when asked for number of search terms, a variable will hold the value. Then a while loop will compare the value with the length of array. If the array is less than the value(the terms in array are less than the number entered by the user), the loop will ask the user for a string search_term and append the string into array.
I'm trying to make a google image downloader which can ask user for their search terms.
Thanks in advance
Solution
you forgot initializing of search_keyword
and int conversion of maxlist
. This works:
import sys
# 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:
search_keyword = list()
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) < int(maxlist):
item = input("Enter Your Item >> ")
search_keyword.append(item)
except Exception as e:
print(str(e))
else:
try:
search_keyword = list()
maxterms = raw_input("Enter Maximum Terms By You[EX: 1/2/3] >> ")
maxlist = maxterms
while len(search_keyword) < int(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
Answered By - Sinapse
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.