Why does my code stop after I ask for input

Question:
My code is being weird and I have spend a long time trying to fix it, can anyone help?

Current behavior:
Somewhere after line 151 is ran, it stops and doesn’t even end the program. It just continues to ask for an input.
Desired behavior
After you enter your input, it should start a practice.

Repl link:
https://replit.com/@randomcat962/Type-in-Japanese?v=1
Sorry if this is the wrong link. I’m not sure which link I’m supposed to add.

def katakana_practice():
  def multi_character_variable():
    character_lengths = [1, 2, 3, 4, 5]
    character_lenth = random.choice(character_lengths)

    if character_lenth == 1:
      output = random.choice(katakana)
    elif character_lenth == 2:
      output = random.choice(katakana) + random.choice(katakana)
    elif character_lenth == 3:
      output = (
        random.choice(katakana) + random.choice(katakana) + random.choice(katakana)
      )
    elif character_lenth == 4:
      output = (
        random.choice(katakana)
        + random.choice(katakana)
        + random.choice(katakana)
        + random.choice(katakana)
      )
    else:
      output = (
        random.choice(katakana)
        + random.choice(katakana)
        + random.choice(katakana)
        + random.choice(katakana)
        + random.choice(katakana)
      )

    return output

  finished_practicing = False
  first_question = True
  character = "ND"
  print(
    "Would you like to practice one character at a time, or several? Enter '1' to do one character at a time, or '+' to do more."
  )
  user_input = input()

  if user_input == "1":
    while finished_practicing == False:
      if first_question:
        character = random.choice(katakana)
        first_question = False

      print("Please enter", character)
      user_input = input()
      if user_input == character:
        character = random.choice(katakana)
        print(
          "Correct! Enter 'X' to end the program. If you want to continue, you can press enter."
        )
        user_input = input()

        if user_input == "X":
          finished_practicing = True
      else:
        print("That's incorrect.")
  if user_input == "+":
    while finished_practicing == False:
      if first_question:
        character = multi_character_variable()
        first_question = False

    print("Please enter", character)
    user_input = input

    if user_input == character:
      character = multi_character_variable()
      print(
        "Correct! Enter 'X' to end the program. If you would like to continue, please press the enter key."
      )
      user_input = input()

      if user_input == "X":
        finished_practicing = True

    else:
      print("That's incorrect")
1 Like

Code Revise & Revision

So below I have gone ahead and broken down several problems you have in your script. Please take time to review these for future reference. I would also advise you take the 100 Days of Python course for Replit if you have not already! This is a really good start for things like loop conditions.

  1. Indentation Error: In your code, there’s a small issue with the way the print statement is indented inside the if user_input == "+" block. It should be properly aligned with the surrounding code.
  2. Infinite Loop: You’ve got a loop with the condition while finished_practicing == False inside both the if user_input == "1" and if user_input == "+" blocks. The problem here is that there’s no clear way to exit this loop. It will keep running indefinitely. We need to add a condition to stop the loop when the practice session is finished.
  3. Input Function: There’s a small typo when you’re getting user input. Instead of user_input = input, it should be user_input = input(). The parentheses are important to call the input function.
  4. Inconsistent Variable Names: You have some variable names with typos, like character_lenth, which should be corrected to character_length, and character_lengths, which should be character_lengths to maintain consistency.

Updated & Tested Code

Below I will provide you the new python script where these issues have been addressed accordingly. It is important to note the changes provided may not meet your specific needs but this is should be used as a learning guide for the future.

import random

katakana = ["カ", "キ", "ク", "ケ", "コ", "サ", "シ", "ス", "セ", "ソ"]  # You can add more katakana characters here

def multi_character_variable():
    character_lengths = [1, 2, 3, 4, 5]
    character_length = random.choice(character_lengths)
    output = "".join(random.choice(katakana) for _ in range(character_length))
    return output

finished_practicing = False
first_question = True
character = "ND"

print("Would you like to practice one character at a time, or several? Enter '1' to do one character at a time, or '+' to do more.")
user_input = input()

if user_input == "1":
    while not finished_practicing:
        if first_question:
            character = random.choice(katakana)
            first_question = False

        print("Please enter", character)
        user_input = input()
        if user_input == character:
            character = random.choice(katakana)
            print("Correct! Enter 'X' to end the program. If you want to continue, you can press enter.")
            user_input = input()

            if user_input == "X":
                finished_practicing = True
        else:
            print("That's incorrect.")

elif user_input == "+":
    while not finished_practicing:
        if first_question:
            character = multi_character_variable()
            first_question = False

        print("Please enter", character)
        user_input = input()

        if user_input == character:
            character = multi_character_variable()
            print("Correct! Enter 'X' to end the program. If you would like to continue, please press the enter key.")
            user_input = input()

            if user_input == "X":
                finished_practicing = True
        else:
            print("That's incorrect.")

print("Practice session ended.")
3 Likes