Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Help
#1
Hi everyone,

I need help writing a python condition. I cannot use loops (while conditions), and must stick to else/if/elif conditions for it.

It's a guessing game - I need to allow the user to have 4 attempts, and if they don't guess the number after 4 attempts it tells them "You're out of guesses! The number was XX"

I'm having trouble figuring out how to only allow the user to have 4 guesses and then printing the response above. Any help is appreciated
import random
import math
winning_number = math.floor(20*random.random()) + 1
print("number_chosen = " , winning_number)
guesses_remaining = 4
i = 1
print("Guess a number between 1 and 20: ")
guess = float(input())

if (guess == winning_number) :
  print("You win! The number is" , winning_number)

else:
    if (guess < winning_number) :
        print("That's too low! Try again!")
        guess = float(input())
    if (guess > winning_number) :
        print("That's too high! Try again!")
        guess = float(input())
    if (guess == winning_number) :
        print("You win! The number is" , winning_number)
    else:
        if (guess < winning_number) :
            print("That's too low! Try again!")
            guess = float(input())
        if (guess > winning_number) :
            print("That's too high! Try again!")
            guess = float(input())
        if (guess == winning_number) :
            print("You win! The number is" , winning_number)         
        else:
            if (guess < winning_number) :
                print("That's too low! Try again!")
            guess = float(input())
            if (guess > winning_number) :
                print("That's too high! Try again!")
            guess = float(input())
            if (guess == winning_number) :
                print("You win! The number is" , winning_number)
if (guesses_remaining != winning_number) :
    print("You are out of guesses, you lose! The number was", winning_number)
Reply
#2
first you have lots of brackets that you don't need, eg:
if guess == winning number:

You could put all of that into a function and call it four times. I don't know if you got to functions in class yet, it might not be allowed.

import random

winning_number = random.randrange(20)
solved = False

def ask_question(chance):
    if chance < 4 and solved == False: 
    """put all the question code here"""
    """put all the question code here"""
    """put all the question code here   If the player gets it right change solved to True"""
    else:
        print("You are out of guesses, you lose! The number was", winning_number)

ask_question(1)
ask_question(2)
ask_question(3)
ask_question(4)
You'll have to make it work, but it doesn't use while loops.
Reply
#3
If you can't use loops, you can use recursion. Since it should only recurse 3 times, there won't be a stack overflow problem. Here's an example of recursion:

def recurse(x):
    print(x)
    if x > 0:
        recurse(x - 1)

recurse(4)
That will output this:
Output:
4 3 2 1
Reply


Forum Jump:

User Panel Messages

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