-4

I am in my first weeks of coding and am trying to solve an issue in my code

the challenge: how to inform user that his input is not in the alphabet and subsequently return him to the input line without continuing the code.

the code i have

import random
import re

print("    _______\\__")
print("   (_. _ ._  _/")
print("    '-' \__. /")
print("         /  /")
print("        /  /    .--.  .--.")
print("       (  (    / '' \/ '' \              ||  ||   /\   |\  |  _____ |\  /|   /\  |\  |")
print("        \  \_.'            \   )         ||--||  /__\  | \ | | ___  | \/ |  /__\ | \ |")
print("        ||               _  './          ||  || /    \ |  \| |____| |    | /    \|  \|")
print("         |\   \     ___.'\  /")
print("           '-./   .'    \ |/")
print("              \| /       )|\'")
print("               |/       // \\")
print("               |\    __//   \\__")
print("              //\\  /__/     \__|")
print("          .--_/  \_--.")
print("         /__/      \__\'")

name_user = str(input("What is your name?:"))
print("Hello,", name_user, "lets play HangMan, try to guess the word i have challenged you with?")

word_list = ["fireboard", "identical", "chocolate", "christmas", "beautiful", "happiness", "wednesday", "challenge", "celebrate"]

random_pick = random.choice(word_list)
random_pick_a = re.sub("[a-z]","*", random_pick)
random_pick_list_a = list(random_pick_a)
print(random_pick)
count = 0


def main_function():
    global count
    while count <= 9:
        user_input = str(input("type a letter:"))
        for i, c in enumerate(random_pick):
            if c == user_input.casefold():
                random_pick_list_a[i] = user_input.casefold()
                random_pick_list_b = ''.join(random_pick_list_a)
                if random_pick_list_b == random_pick:
                    print("done")
                    exit()
                else:
                    continue
        else:
            if user_input.casefold() not in random_pick:
                count = count+1
                print(count)
                if count == 10:
                    print("sorry")
                    exit()
            print(random_pick_list_b)


main_function()
Eelco
  • 49
  • 1
  • 3
  • 2
    The cute banner doesn't really belong here; see also the guidance for creating a [mre]. – tripleee Jan 07 '20 at 15:12
  • 2
    Making the variable `global` makes no sense at all; you are not using it outside your function. You should generally avoid global variables. – tripleee Jan 07 '20 at 15:13

1 Answers1

0
import string

def get_valid_answer(prompt, errormessage):
    answer = ""
    while True:
        answer = input(prompt)
        if answer not in string.ascii_lowercase:
            print(errormessage)
        else:
            return answer

used like this:

get_valid_answer("Make a guess!:","Sorry mate, pick a lower case letter.")

example run:

Make a guess!:1
Sorry mate, pick a lower case letter.
Make a guess!:F
Sorry mate, pick a lower case letter.
Make a guess!:a

(function returns 'a')

Christian Sloper
  • 7,440
  • 3
  • 15
  • 28