python - Can anyone help me with code below? -


i have code application that, example teachers, can use check students reading ages. there part asks user input how many keywords want i'm confused / stuck on.

my code:

counter = 0  amount_keywords = []  input_keywords = int(input("enter number of keywords:"))  while(counter != input_keywords):  input_keywords = str(input("enter keyword:"))  amount_keywords.append(input_keywords)  counter = counter + 1 

whats happening if put 0 onto integer "enter number of keywords:" works fine , passes next section asking put keywords in, perfect , working right. if put example number 3 should come string "enter keyword:" 3 times because that's i've put integer instead out carries on going. anyway fix this?

you have used variable input_keywords hold number of keywords input redefine it, inside while loop, hold each keyword. thus, if enter 3 before loop, type keyword 'a' during loop, input_keywords = 'a' when while loop compares counter, never equal, keeps going. try naming things more , don't reuse variables different purposes. example:

counter = 0 amount_keywords = [] n_keywords = int(input("enter number of keywords:")) while(counter != n_keywords):     keyword = str(input("enter keyword:"))     amount_keywords.append(keyword)     counter = counter + 1 

to debug things yourself, add print statement in while loop see value of counter , input_keywords , see why loop-ending condition not being met. or use debugger lets step through code , @ variable values.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -