Python number game with changing feeback -
Python number game with changing feeback -
user guesses 4 digit number , feedback needs 'f' if number right not in right place, 'x' if number not in number @ , if digit right , in right position displays digit. code below shows effort giving me error: expected str instance, list found
from random import randint def check_char(a, b): #function making output display f, number , x depending on user input if == b: homecoming randomnumber #number displayed when correctly guessed elif b == randomnumber: homecoming 'f' #f means number somewhere in randomnumber elif b != randomnumber: homecoming 'x' #x means number in randomnumber guessestaken = 1 randomnumber = [str(randint(1, 9)) _ in range(4)] # create list of random nums print(randomnumber) while guessestaken < 10: guesses = list(input("guess number: ")) # create list of 4 digits check = ''.join([check_char(int(a), int(b)) a, b in zip(guesses, randomnumber)]) if check == "yyyy": # if check has 4 y's have right guess print("congratulations, correct, took you", guessestaken, "guesses.") break else: guessestaken += 1 # else increment guess count , inquire 1 time again print(check) if guessestaken == 10: print("you lose")
you replacing x f if user has guessed number , in randomnumber @ position replaced ``f:
from random import randint guessestaken = 1 randomnumber = [str(randint(1, 9)) _ in range(4)] # create list of random nums def check_char(a, b): #function making output display f, number , x depending on user input if == b: homecoming b #number displayed when correctly guessed elif in randomnumber , != b: # if homecoming 'f' elif != b: homecoming 'x' while guessestaken < 10: guesses = list(raw_input("guess number: ")) # create list of 4 digits check = ''.join([check_char(a, b) a, b in zip(guesses, randomnumber)]) if check.isdigit(): # if check digits, number guessed print("congratulations, correct, took you", guessestaken, "guesses.") break else: guessestaken += 1 # else increment guess count , inquire 1 time again print(check) else: print("you lose")
you don't need cast ints in check_char(a, b)
strings compare fine "1" == "1" true
you returning list return randomnumber
not substring b
error trying join
expects string not list, b
never equal randomnumber
.
python numbers
Comments
Post a Comment