Confusion with variable types

Lol, now I’m the one needing to be helped :sweat_smile:
So I challenged myself to make a game without any indentation/tabs and started making hangman. The project was doing well so far until…

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    while wordArr != list(word): os.system('clear'); print(stickString if wrongCount < len(stickString) else stickString[-1]); print(f"Count: {count}"); print(wordArr); x = input(""); wrongCount = compare(word, x, wrongCount); count += 1
TypeError: '<' not supported between instances of 'list' and 'int'

And what’s odd about this is because I checked the variable types before the while loop started, and both are showing up as integers. Both len(stickString) and wrongCount are integers.

Since it was only checking their types before the while loop starts, my line of thinking is that something is happening that is inadvertently turning something into a list. But I can’t see in the code where that could possibly happen! I hope any of you can help me in this predicament.

Here’s the code:

import random, os;

word, stickString, wordArr, count, wrongCount = random.choice(["apple", "dogs", "something"]), "  ____\n |    |\n      |\n      |\n      |\n     _|_", [], 0, 0

for i in range(len(word)): wordArr.append("_")
  
compare = lambda word, x, wrongCount: [(z := i, wordArr.__setitem__(z, x)) for i, letter in enumerate(word) if letter == x] or wrongCount + 1

while wordArr != list(word): os.system('clear'); print(stickString if wrongCount < len(stickString) else stickString[-1]); print(f"Count: {count}"); print(wordArr); x = input(""); wrongCount = compare(word, x, wrongCount); count += 1
  
os.system('clear')
print(wordArr)

I created spaces between some of the lines for readability for you all.

note: (any fixes for this has to not need indentation for new lines, because that’s the point of the challenge)

https://replit.com/@DefendyPug/Game-without-Indentation

I can’t seem to get this error? Any steps to re-create it?

Play the game, and guess letters. if you ever guess one of the correct letters, the error will show up.

Can’t you just put the entire thing into exec or eval? Also I gave this a try :wink:

a,c=input(),'_';b,d,e=a,len(a),lambda:print(*[a[i]if b[i]==c else c for i in range(d)])
while b!=c*d:exec("e();g=input()\nfor i in g:b=b.replace(i,c)if i in a else b")
e();print('You win!')

You have to start by typing in a word though, and the UI is pretty bad

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    eval(code)
  File "<string>", line 4
    word, stickString, wordArr, count, wrongCount = random.choice(["apple", "dogs", "something"]), "  ____
                                                                                                   ^
SyntaxError: unterminated string literal (detected at line 4)

it evaluated weirdly

Did you put a \n after each line? Unterminated string literal means that you starter writing another line immediately after a number I think

are you allowed to use multi-lined strings for eval? Because thats what i did

You didn’t link the game.

Not sure, never tried it :man_shrugging:

made link in edit. I hope this helps

`exec` and `eval` are not recommended

2 Likes

For the purposes of solving this challenges. Obviously writing code without indentation also isn’t recommended

2 Likes

It’s because your compare() function sometimes returns a list containing a tuple. I’m not sure how the compare function works, so I don’t know why this is happening but it explains why wrongCount isn’t an int
image

2 Likes

I’m going to check it out, thank you!

Thank you so much! Yeah the compare method was for some reason turning wrongCount into an int. I had to fix acouple issues afterwards by myself but they weren’t as important, just user fault by me.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.