0

So I've been learning python and I'm trying to make my first game to just try the language on my own and get to grips with it. While making this word guessing game, I ran in to an issue with input which I am finding it difficult to overcome.

import sys
import os
import random
import time

word_list = ["salt","excess","product","rib","slot","battle"]

#random word from list
random_word = random.choice(word_list)
#defining word_len and making it a string so it can be used in print()
word_len = len(random_word)
word_len = str(word_len)

print("Welcome to the guessing  game")
print("I will give you the length of a random word and you need to guess 
what it is!")
print("The word is " + word_len + " characters long.")
print("The choices are " + "salt " + "excess " + "product" + " rib" + " 
slot" + " and battle" )
print("Good luck!")

#reads the input and defines it
word_guess = sys.stdin.readline()


if word_guess is random_word :
print("Congratulations! You guessed correctly!")
#else which is running despite if being met
else :
print("You lost! :( The word was " + random_word)
#sleep for 5 seconds so the terminal doesnt close
time.sleep(5)

The problem is when running the code it produces this: https://i.stack.imgur.com/tv7WN.png If anyone could share what I need to change I'd appreciate it

Fergy323
  • 33
  • 5

4 Answers4

0

The changed code (only the if else part). readline keeps the newline in the string. So, we should remove that before comparison.

word_guess = sys.stdin.readline()
word_guess = word_guess[:-1]
if random_word == word_guess :
  print("Congratulations! You guessed correctly!")
#else which is running despite if being met
else :
  print("You lost! :( The word was " + random_word)
sgarg
  • 184
  • 4
  • So the .readline() means that it will stay the same, by changing it to equal [:-1] it means it's reading the last character in the string if I'm not mistaken? Just trying to wrap my head round this – Fergy323 Nov 03 '18 at 09:52
  • It's reading the string from 0th index to the "last - 1"th index, which is equivalent to removing the last character, i.e. removing the newline character. After the removal of the newline, the random word and the guessed words are matched using ==. – sgarg Nov 03 '18 at 10:51
0

Python difference between is and equals(==) The is operator may seem like the same as the equality operator but they are not same. The is checks if both the variables point to the same object whereas the == sign checks if the values for the two variables are the same.

I would like to point out one more flaw in your code.

word_list = ["salt","excess","product","rib","slot","battle"]
#Battle and excess is of same length
#slot and salt is of same length

#random word from list
random_word = random.choice(word_list)
word_len = len(random_word)
word_len = str(word_len)
#As you pick this random, chances of picking length 4 and word 'salt' is good. Whereas you could predict is as 'slot' and get an unexpected error even though you are correct.  

So, modify the list like:

word_list = ["salt","excess","product","rib"]

Sample erroneous output, even after changing the 'is' to '==' in the code:

Welcome to the guessing  game
I will give you the length of a random word and you need to guess what it is!
The word is 4 characters long.
The choices are salt excess product rib slot and battle

Enter the word : salt
Good luck!
You said salt
You lost! :( The word was slot

SLOT and SALT both are 4 letters :) Feel free to implement some other logic here.

Jim Todd
  • 1,488
  • 1
  • 11
  • 15
0

reads the input and defines it

word_guess = sys.stdin.readline() work_guess has the value \n appended in the end so that why it is not matching.

replace if word_guess is random_word :

by if word_guess[0:-1] == random_word :

It will work.

Also please click here to see == and is in python.

Community
  • 1
  • 1
  • Thank you, yeah I now realise the mistake I made using 'is' as opposed to ==. In the beginning I did have == but tried Is just to see if it would work but I realise now that was a silly mistake. – Fergy323 Nov 03 '18 at 09:50
  • @Fergy323 Have a check on my answer too, where I have pointed one more error of words of same length like SALT and SLOT. That will help you refine your code more to perfection. Thanks. – Jim Todd Nov 03 '18 at 10:07
0

you have to specify EOF if you want to use sys.stdin.readlines(), else you can read the input simply by input prompt, both works well.EOF is Needed `

guru bhat
  • 1
  • 1