Python Forum
I don't know if it's my error or Python error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't know if it's my error or Python error
#1
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]
Reply
#2
Convert the string into an integer and not vice versa:

guess = int ( input("a number: ") )
if guess == x:
...
Reply
#3
I converted my input string to int but it's still doing the same thing.
Reply
#4
Then show us the new code
Reply
#5
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.")
Reply
#6
#!/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.")
Reply
#7
You're a genius!!!!! Thank you!
Reply
#8
What is still to do?
    the user should end the program
    until now, the game runs forever
Reply
#9
# 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.")
Reply
#10
Good work.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QGIS 3.34 Python Error paskiller 2 656 Nov-13-2023, 06:20 PM
Last Post: buran
  Python error on mentioned Arduino port name dghosal 5 846 Aug-22-2023, 04:54 PM
Last Post: deanhystad
  Code error from Fundamentals of Python Programming van Richard L. Halterman Heidi 12 1,681 Jul-25-2023, 10:32 PM
Last Post: Skaperen
Thumbs Down Python Error MahendraYadav 0 333 Jul-21-2023, 05:07 PM
Last Post: MahendraYadav
  Syntax error while executing the Python code in Linux DivAsh 8 1,540 Jul-19-2023, 06:27 PM
Last Post: Lahearle
Sad "PriceSystem" Python Script error to Shopify API Alphetto 0 434 Jul-04-2023, 10:03 AM
Last Post: Alphetto
  Error when running kivy on python janeik 8 2,025 Jun-16-2023, 10:58 PM
Last Post: janeik
  error python audio codiac 3 4,115 Mar-30-2023, 03:12 PM
Last Post: deanhystad
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,551 Mar-27-2023, 07:38 AM
Last Post: buran
  Getting "SSL client not supported by this Python installation" error prabirsarkar 0 947 Mar-13-2023, 05:01 PM
Last Post: prabirsarkar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020