Python Forum
I don't know if it's my error or Python error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: I don't know if it's my error or Python error (/thread-6490.html)

Pages: 1 2


I don't know if it's my error or Python error - SkitzGerald - Nov-25-2017

import random
from random import randint
x = [random.randint(1,100)]

print("Guess a number between 1 and 100.")

while True:
	guess = input("")
	if str(guess) == str(x):
		print("Congratulations!")
	elif str(guess) < str(1) or str(guess) > str(100):
		print("You have to choose a number between 1 and 100.")
		print("Please guess again.")
	elif str(guess) < str(x):
		print("Please choose a higher number.")
	elif str(guess) > str(x):
		print("Please choose a smaller number.")

#The problem is, is when I type in any number from 10 - 99 it forces the first elif statement to resolve and tells me to pick a number between 1 and 100. However if I type in 100 it tells me to pick a higher number.
When I change str(100) to str(99) the program works until I input 100 or higher then it tells me to pick a higher number. What am I doing wrong?
[python]
[/python]


RE: I don't know if it's my error or Python error - heiner55 - Nov-25-2017

Convert the string into an integer and not vice versa:

guess = int ( input("a number: ") )
if guess == x:
...



RE: I don't know if it's my error or Python error - SkitzGerald - Nov-25-2017

I converted my input string to int but it's still doing the same thing.


RE: I don't know if it's my error or Python error - heiner55 - Nov-25-2017

Then show us the new code


RE: I don't know if it's my error or Python error - SkitzGerald - Nov-25-2017

import random
from random import randint


x = [random.randint(1,100)]

print("Guess a number between 1 and 100.")

while True:
	guess = int ( input("a numer: ") )
	if str(guess) == str(x):
		print("Congratulations!")
	elif str(guess) < str(1) or str(guess) > str(100):
		print("You have to choose a number between 1 and 100.")
		print("Please guess again.")
	elif str(guess) < str(x):
		print("Please choose a higher number.")
	elif str(guess) > str(x):
		print("Please choose a smaller number.")



RE: I don't know if it's my error or Python error - heiner55 - Nov-25-2017

#!/usr/bin/python3
import random
from random import randint

x = random.randint(1,100)   # <== without [  ]
print("Guess a number between 1 and 100.")
 
while True:
    guess = int(input(""))
    if guess == x:
        print("Congratulations!")
    elif guess < 1 or guess > 100:
        print("You have to choose a number between 1 and 100.")
        print("Please guess again.")
    elif guess < x:
        print("Please choose a higher number.")
    elif guess > x:
        print("Please choose a smaller number.")



RE: I don't know if it's my error or Python error - SkitzGerald - Nov-25-2017

You're a genius!!!!! Thank you!


RE: I don't know if it's my error or Python error - heiner55 - Nov-25-2017

What is still to do?
    the user should end the program
    until now, the game runs forever


RE: I don't know if it's my error or Python error - SkitzGerald - Nov-25-2017

# I'm trying to figure out how to end it now.  This is what I have so far but it's not working.
import random
from random import randint


x = random.randint(1,100)

print("Guess a number between 1 and 100.")

active = True
while active:
	guess = int(input(""))
	if guess == x:
		print("Congratulations! Would you like to play again?")
		play_again = input("")
		if play_again.lower() == 'yes' or 'y':
			print("Guess a number between 1 and 100.")
		elif play_again.lower() == 'no' or 'n':
			print("Thanks for playing")
			active = False
	elif guess < 1 or guess > 100:
		print("You have to choose a number between 1 and 100.")
		print("Please guess again.")
	elif guess < x:
		print("Please choose a higher number.")
	elif guess > x:
		print("Please choose a smaller number.")



RE: I don't know if it's my error or Python error - heiner55 - Nov-25-2017

Good work.