-1

So im making a guessing game where the computer picks a randomint between 1 - 100 and when guessing if you guess below the number it will tell you to go higher and if you guess above the number it will tell you to guess lower. It works for the most part it's just when I guess 100 it tells me to go higher.

Now before you start the game the computer will add a random number to an array and all you need to do is match the number provided in the array.

I've tried switching the array to a set but then i get this error message: "'dict' object has no attribute 'add'"

That is the code i am working with.

Also when trying to write to a file it doesn't seem to work too well

from random import *
from random import randint
import os

numbers = []
Guesses = []

os.system('CLS')
print("I want you to guess my number between 1 - 100")

f = open("Data.txt", "a")

print("Say 'yes' If You're Ready")

YN = input()



if YN == "yes":
    os.system('CLS')
    for _ in range(1):
        value = randint(1, 101)
        numbers.append(value)
        break



while True:
    Guess = input()
    Guesses.append(Guess)
    print(numbers)

    if Guess < str(value):
        print("Higher")

    elif Guess > str(value):
        print("Lower")

    elif Guess == str(value):
        os.system('CLS')
        f.write(str(Guesses))
        print("That Was Correct!")
        for x in Guesses:
            print(x)
        break

input()


Lazer
  • 73
  • 1
  • 1
  • 8

2 Answers2

0
  • Your randint end is set to 101 (see randint() function), so in the case when value = 101 and you are guessing 100 your code correctly states to go higher.
  • The opened file is not closed, that may cause the file handling issues you've mentioned.
  • The comments about int/str comparisons are shown in an example and the end of below revised code

Here is your code slighlty revised and some comments added to show above items:

from random import randint
import os

numbers = []
Guesses = []

os.system('CLS')
print("I want you to guess my number between 1 - 100")

f = open("Data.txt", "a")
try:
    print("Say 'yes' If You're Ready")

    YN = input()

    if YN == "yes":
        os.system('CLS')
        for _ in range(1):
            value = randint(1, 101)
                # start end is set from 1 to 101 (not to 100)
            numbers.append(value)
            print('DEBUG: Single item in array: ' + str(numbers) + '\n')
                # for loop is only done once as mentioned by Austin
                # and cheating for debugging :)
            # break  # not required as mentioned by Austin

    while True:
        print('Take a guess:')
        Guess = int(input())  # directly convert input to integer
        Guesses.append(Guess)
        print(numbers)

        if Guess < value:  # comparison on integer as Austin mentioned
            print("Higher")
        elif Guess > value:
            print("Lower")
        elif Guess == value:
            os.system('CLS')
            f.write(str(Guesses))
            print("That Was Correct!")
            for x in Guesses:
                print(x)
            break

    print('Guesses: ' + str(Guesses))
finally:
    f.close()
# input()  # not required

# Amendment: showing string sorting mentioned by jonrshape
hundred = '100'
two = '2'
if hundred < two:
    print('\nWrong, 100 is not smaller than 2.')
hundred = int(hundred)
two = int(two)
if hundred > two:
    print('Correct, 100 is larger than 2.')
MagnusO_O
  • 1,202
  • 4
  • 13
  • 19
-2

@jonrsharpe explained that strings are sorted lexicographically meaning that it put the word 'one hundred' into alphabetical order.

Lazer
  • 73
  • 1
  • 1
  • 8