Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
guessing script
#1
Hello, this is my first time in python and i need help with my code, i want to guess a number between 1-100, but the problem is i can only guess 1-3times , i want to guess until i get the correct answer, can you help me?

import random

gissat = 0

rätt = random.randint(1, 100)
print("Gissa talet:")

while gissat<rätt:
    gissat = input()
    gissat = int(gissat)

    
    if gissat<rätt:
        print ("För litet!")
   
    if gissat>rätt:
        print ("För stort!")

    if gissat==rätt:
        break
Reply
#2
You break out of the code when correct so you want to loop continously.

Change this
while gissat<rätt:
To this:
while True:
Reply
#3
Try to write in English in code.
It's okay to use Swedish in output part that user see.

Using !=(Not equal) and it will natural break out of the loop,
when right number is guessed.
Eg.
import random

print("Gussing Game")
secret_number = random.randint(1, 100)
gissat = 0
while gissat != secret_number:
    gissat = int(input('Gissa talet: '))
    if gissat < secret_number:
        print("För litet!")
    if gissat > secret_number:
        print("För stort!")

print('You gussed it the number was {}'.format(secret_number))
Reply
#4
thank you so much, this solved my problem, and im sorry for writing in swedish, didnt think about it lol, next time i will translate before :D
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Play again, in a guessing game banidjamali 4 11,638 Jan-22-2021, 06:23 AM
Last Post: banidjamali
  Guessing game with 4 different digits LinseyLogi 6 3,615 Mar-29-2020, 10:49 AM
Last Post: pyzyx3qwerty
  Name guessing game in python Liquid_Ocelot 6 14,977 Apr-01-2017, 12:52 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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