0

I'm using python3 on mac. I'm currently doing a project. However, I was trying to use "while = True" to continuously use the program until a condition is met. Please, tell me what am I missing in my code. Thanks!

import json
import difflib
from difflib import get_close_matches
 data = json.load(open("project1/data.json"))
 word = input("Enter a word or enter 'END' to quit: ")
def keyword(word):
 word = word.lower()
 while type(word) == str:
 if word in data:
 return data[word] 
 elif word == 'END'.lower():
 break
 elif len(get_close_matches(word, data.keys())) > 0:
 correction = input("Did you mean %s insted? Enter Yes of No: " % get_close_matches(word, data.keys())[0])
 if correction == "Yes".lower():
 return data[get_close_matches(word, data.keys())[0]]
 elif correction == "No".lower():
 return "This word doesn't exist. Plese enter again. "
 else:
 return "Please enter 'Yes' or 'No: "
 else:
 return "This word doesn't exist. Please enter again."
 print("Thanks!")
output = (keyword(word))
if type(output) == list:
 for item in output:
 print(item)
else:
 print(output)
asked May 22, 2020 at 10:40
1

2 Answers 2

1

I think this might be the setup you are looking for.

def keyword(word):
 if word in data:
 return data[word]
 elif len(get_close_matches(word, data.keys())):
 correction = input(f"Did you mean {get_close_matches(word, data.keys())[0]} instead? y/n: ")
 if correction == 'y':
 return data[get_close_matches(word, data.keys())[0]]
 elif correction == 'n':
 return "This word doesn't exist. Please enter again."
 else:
 return "Please type 'y' or 'n': "
 else:
 return "This word doesn't exist. Please enter again."
while True:
 word = input("Enter a word: ").lower()
 if word == 'end':
 print("Thanks!")
 break
 else:
 print(keyword(word))

Looking at the source code and your question, it seems like what you want to achieve is basically to continuously accept input from the user until the user enters something like 'end'. One way to go about this is to separate out the while-loop logic from the function. The overarching while-loop logic is at the bottom half of the code, where we continuously accept input from the user until the user inputs some lower or upper case variant of 'end'. If this condition is not met, we proceed to printing out the result of the function call keyword(word).

Minimal modifications were made to the original keyword() function, but here are a few changes worthy of note:

  • The while type(word) == str is unnecessary, since the result stored from the input() function will always be a string. In other words, the condition will always return True.
  • Having return statements within a while loop defeats the purpose of a loop, since the loop will only be executed once. After returning the specified value, the function will exit out of the loop. This is why we need to separate out the loop logic from the function.
  • Although %s works, it's a relic of C. This might be a matter of personal choice, but I find f-strings to be much more pythonic.
answered May 22, 2020 at 11:18
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your comments. However, when I execute the program it keeps looping, showing the same result multiple times.
Also, I added an if statement so that my results wouldn't show as a list.
0

You are using the worng condition.

type((3,4))== list

is False. You must use

type((3,4)) == tuple

answered May 22, 2020 at 10:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.