Proper way of writing recurring function in python - python-3.x

So I'm new to python and I tried making a random number game generator as an exercise. You need to guess the random number in a limited number of tries. Here's my code.
import random
rand_num = random.randint(1,6)
print("\nNUMBER GUESSING GAME")
lives = 5
def guess():
guess = int(input("Input guess: "))
return guess
print(rand_num) #to see the correct answer
x = guess()
if x == rand_num:
print("Good job! YOU WIN.\n")
else:
while x != rand_num:
if x == rand_num:
print("Good job! YOU WIN.\n")
break
lives -= 1
print("Incorrect. Please try again")
guess()
if lives == 0:
print("YOU LOSE.\n")
break
So if your first guess is wrong, and you found the right answer in the subsequent tries, the game won't recognize it.
Thanks in advance.

You were on the right track. In the future, walk yourself through the program thinking what each line is doing and whether the sequence makes logical sense.
There was only one actual error in your code: you didn't collect a new x value in your code when calling guess in the while loop. The other elements were correct, just out of order. Here is a more logical order, with the collecting issue fixed,
import random
rand_num = random.randint(1,6)
print("\nNUMBER GUESSING GAME")
lives = 5
def guess():
guess = int(input("Input guess: "))
return guess
print(rand_num) #to see the correct answer
x = guess()
if x == rand_num:
print("Good job! YOU WIN.\n")
else:
while x != rand_num:
# 1) Notify the user
print("Incorrect. Please try again")
# 2) Decrement remaining guesses
lives -= 1
# 3) Check if maximum number of guesses
# reached - end if yes
if lives == 0:
print("YOU LOSE.\n")
break
# 4) Get a new guess
x = guess()
# 5) Compare
if x == rand_num:
print("Good job! YOU WIN.\n")
You may want to notify your player what are the ranges of numbers to guess. You can also extend this exercise, and code several difficulty levels, each with a different range (e.g. simple level 1-6, medium 1-20, etc.), to be chosen by the player.
As #Stef mentioned in the comment, it is technically cleaner and more common to include the guess number (lives) condition as part of the while loop condition,
import random
rand_num = random.randint(1,6)
print("\nNUMBER GUESSING GAME")
lives = 5
def guess():
guess = int(input("Input guess: "))
return guess
print(rand_num) #to see the correct answer
x = guess()
if x == rand_num:
print("Good job! YOU WIN.\n")
else:
while (x != rand_num) and (lives > 0):
# 1) Notify the user
print("Incorrect. Please try again")
# 2) Decrement remaining guesses
lives -= 1
# 3) Get a new guess
x = guess()
# 4) Compare, enf if correct
if x == rand_num:
print("Good job! YOU WIN.\n")
# If ending because no more guesses left
if lives == 0:
print("YOU LOSE.\n")
I use brackets for each condition, it is not necessary here and a matter of style. I like to separate conditions this way.
Also, the "YOU LOSE.\n" part is now printed at the end, if the all the guesses were exhausted.
One note: with your code as it is now, your player actually gets to guess 6 times, once in the beginning and 5 more times in the while loop.

Related

i doesnt do what i want it to doo .i am making a guess the num game

from random import *
name = input("what is your name:")
options = input('what difficulty do you want to play on [ easy , normal , hard ]:').lower
random_num = 0
if options == 'easy':
print('ok, you will get numbers between 0 - 7')
random_num = randint(0, 7)
elif options == 'normal':
print('ok, you will get numbers between 0 - 10')
random_num = randint(0, 10)
elif options == 'hard':
print('ok, you will get numbers between 0 - 15')
random_num = randint(0, 15)
else :
print('sorry I am not able to understand please choose one from [ easy , normal , hard ]')
guess = input('ok i am ready guess a number in your range :')
if random_num == guess :
print('Yictory!!! , you won ,your guess was correct')
elif random_num == guess :
print('You lost!!! ,your guess was wrong')
else:
print('unable to interpret')
can some one help me with this as it doesnt go to else at the end and it goes to the second elif
and i am a biggainer so plz help me as soon as possible . its python 3.9.
The main problem is that guess is a string when it comes from input; you'll need to cast it to an integer, as that's what randint gets you.
guess = int(input('ok i am ready guess a number in your range :'))
This will, of course, crash horribly if you input something that's not a number, but that's a different issue.
There are a number of other issues too, such as the if and elif branches at the end being the same (i.e. you'd never get to "You lost"), and that you never call .lower().
To add proper error handling (and let the player guess more than once), I'd maybe reformulate this as
from random import randint
name = input("what is your name:")
option_ranges = {
"easy": 7,
"normal": 10,
"hard": 15,
}
while True:
options = input("what difficulty do you want to play on [ easy , normal , hard ]:").lower()
max_range = option_ranges.get(options)
if max_range: # found a correct option
break
print("sorry I am not able to understand please choose one from [ easy , normal , hard ]")
print(f"ok, you will get numbers between 0 - {max_range}")
random_num = randint(0, max_range)
while True:
try:
guess = int(input("ok i am ready guess a number in your range :"))
except ValueError:
print("unable to interpret")
continue # Go back to the loop
if random_num == guess:
print("Yictory!!! , you won ,your guess was correct")
break
print("You lost!!! ,your guess was wrong")
The code is okay, except for the logic condition: This line
elif random_num == guess :
print('You lost!!! ,your guess was wrong')
is the same as
if random_num == guess :
print('Yictory!!! , you won ,your guess was correct')
Change the elif logic from == to not equal !=;
elif random_num != guess :
print('You lost!!! ,your guess was wrong')
You should be good to go

While True looping the results of a 'if elif' statement

I'm experimenting with While loops and I encountered this:
CODE:
x = int(input('Guess the number 1-10'))
while True:
if (x == 8):
print('yay!')
break
else:
print('No No')
RESULT:
No No
No No
No No
No No
No No
No No
No No
No No
forever until I stop it...
Some people have suggested to use break, but I don't want to stop it on one try when they get it wrong, I want it to give multple tries until they get the right number. What can I do?
You want to ask the user for input in every iteration:
while True:
x = int(input('Guess the number 1-10'))
if (x == 8):
print('yay!')
break
else:
print('No No')

I don't understand this rock paper scissors game

I found this code, and I'm having trouble understanding the lines 'user_hand = rps[int(user_input) - 1]' , 'com_hand = choice(rps)' , ' if rps_dict[user_hand]['strong'] == com_hand:' , 'print_result(**game_result)' , '
Why should I subtract 1 from user_input? Why put rps inside brackets next to choice? Why do you win 'if rps_dict[user_hand]['strong'] == com_hand ? Why if user_hand equal to com_hand? It doesnt make sense to me.. I know '**game_result' means it's a kwark, but I dont know why I need to declare it as a kwarg. I'm sorry if these questions seem stupid, but I'm frustrated.
from random import choice
rps = ('rock', 'paper', 'scissors')
rps_dict = {
'rock': {'strong': 'scissors', 'weak': 'paper'},
'paper': {'strong': 'rock', 'weak': 'scissors'},
'scissors': {'strong': 'paper', 'weak': 'rock'}
}
def print_result(user_score, com_score, win, lose, tie):
print('Result:', end=' ')
if user_score > com_score:
print('You win!')
elif user_score < com_score:
print('You lose...')
else:
print('Draw.')
print('--------------------')
# Print scores
print(f'Your score: {user_score}')
print(f'Computer score: {com_score}')
print('--------------------')
# Print statistics
print(f'Win: {win}')
print(f'Lose: {lose}')
print(f'Tie: {tie}')
def play(rounds):
game_result = {
'user_score': 0,
'com_score': 0,
'win': 0,
'lose': 0,
'tie': 0
}
while rounds > 0:
user_input = input(
'Enter your choice (1: Rock, 2: Paper, 3: Scissors, 0: Quit): ')
# Check whether the input is in the options
if user_input in ('1', '2', '3'):
rounds -= 1
user_hand = rps[int(user_input) - 1]
com_hand = choice(rps)
# user_hand is strong against com_hand
if rps_dict[user_hand]['strong'] == com_hand:
game_result['user_score'] += 1
game_result['win'] += 1
result = 'You win!'
# user_hand is weak against com_hand
elif rps_dict[user_hand]['weak'] == com_hand:
game_result['com_score'] += 1
game_result['lose'] += 1
result = 'You lose...'
# Tie
else:
game_result['tie'] += 1
result = 'Tie.'
print(
f'You -> {user_hand}. Computer -> {com_hand}. {result}')
elif user_input == '0':
break
else:
print('Invalid input!')
print()
print_result(**game_result)
if __name__ == "__main__":
print('Welcome to Rock-Paper-Scissors Game!')
try:
rounds = int(input('How many rounds you want to play? '))
play(rounds)
except ValueError:
print('Please input a valid number!')
This is an interesting question, and I'll do my best to discuss the lines that you said you have trouble with:
user_hand = rps[int(user_input) - 1]
The user_hand variable is used to store the choice the user inputs. user_input is the text the user directley entered. This will be saved as a string, so the code stransforms it into an integer with the int class. Next, it subtracts one from the user input (computers count from zero rather than 1). Then, it grabs the element from the list that has that index. For example, if I entered one, it would grab the list item at index 0 ("rock").
com_hand = choice(rps)
This line here is used to gather the computer's choice. As you can see from the first line, the choice function from the random module is imported directly. This allows you to use the function without specifying the module it came from. The choice function selects a random item from the specified list, which is the same rps list as before.
if rps_dict[user_hand]['strong'] == com_hand:
The if statement here gathers data from the rps_dict and compares it to the computer's hand. The rps_dict is a dictionary that contains data on which hand beats or loses to another. To translate the if statement into simpler english, it means if the hand the user's hand would beat (which can be found with rps_dict[user_hand]["strong"]), is the computer's hand, the user will win. In addition, to avoid confusion, the == operator check's for equality, and doesn't assign the variable to com_hand.
print_result(**game_result)
Here you said that you don't understand why the parameters are passed in this way. You don't really need to do it in this format, but the person who created the script decided to (possibly as it is simpler to read).
Thank you, and if you have any other questions, please comment and I'll do my best to answer them!

How to fix tic tac toe...it gives incorrect winner..(python)

I'm creating a simple tic-tac-toe game in the python shell...the program incorrectly assigns a winner no matter what i try to edit the code...I'm having a problem with my check() function
I tried scanning the vertical,then horizontal,then the diagonals of the gameboard after each player turn and then tell the winner if the first value matches with the other two...
i have also accounted errors for invalid input
Here's the full code:
import os
import time
board = [['-','-','-'],
['-','-','-'],
['-','-','-'],]
def markit(row,col,mark):
try:
if board[row-1][col-1]!='-':
print("Already Marked!!")
time.sleep(1)
else:
board[row-1][col-1]=mark
except IndexError:
print("Out of Range...Reverting back")
time.sleep(1)
def reset():
board = [['-','-','-'],
['-','-','-'],
['-','-','-'],]
def check():
for i in range(len(board)):
if board[i][i]==board[i-1][i-1] and board[i][i]!='-':
print(board[i][i]," is a winner")
time.sleep(1.5)
y=input("Play Again?(y/n):")
if y==y:
reset()
else:
quit()
break
for i in range(len(board)):
for j in range(len(board)):
if board[j][i]==board[j][i-1] and board[j][i]!='-':
print(board[j][i]," is a winner")
time.sleep(1.5)
y=input("Play Again?(y/n):")
if y==y:
reset()
else:
quit()
break
for i in range(len(board)):
for j in range(len(board)):
if board[i][j]==board[i][j-1] and board[i][j]!='-':
print(board[i][j]," is a winner")
time.sleep(1.5)
y=input("Play Again?(y/n):")
if y==y:
reset()
else:
quit()
break
while True:
print(" 1 2 3")
for i in range(len(board)):
print(i+1,board[i])
row=int(input("P1||Enter row:"))
col=int(input("P1||Enter col:"))
markit(row,col,mark='X')
check()
os.system('cls')
print(" 1 2 3")
for i in range(len(board)):
print(i+1,board[i])
row=int(input("P2||Enter row:"))
col=int(input("P2||Enter col:"))
markit(row,col,mark='O')
check()
os.system('cls')
I expected to tell the winner properly but it tells a winner eventhough my first value doesn't match the other two.
and also if a player gives an invalid input...it returns an error and the match continues but he misses his turn!!...i also want to rectify this.
Briefly here are your mistakes.
if board[i][j]==board[i][j-1] and board[i][j]!='-':
Firstly, you dont need to check for board[i][j]!='-' at every instance. It would be sufficient to check if starting value in the row is not empty using if board[i][0] != '-': instead.
Secondly, the line you wrote checks if 2 adjacent cells are equal in value, and if they are it calls the win. You need to check if all the 3 adjacent values are equal and then only call winner.
To get this behaviour, one way would be to declare a boolean won. If any point, you get 2 adjacent values different, you set won = False and break.
At the end of loop, if won == True you declare the winner and do things accordingly.
Moreover, in your main method, you should invoke check() after printing the board, not before. Like this:
row=int(input("P1||Enter row:"))
col=int(input("P1||Enter col:"))
markit(row,col,mark='X')
os.system('cls')
print(" 1 2 3")
for i in range(len(board)):
print(i+1,board[i])
check()
I have fixed the horizontal check for you. You can try fixing the vertical and diagonal yourself.
won = True
for i in range(len(board)):
if board[i][0] != '-':
for j in range(len(board)-1):
if board[i][j]!=board[i][j+1]:
won = False
break
if won:
print(board[i][i]," is a winner")
time.sleep(1.5)
y=input("Play Again?(y/n):")
if y=='y':
reset()
else:
quit()
return
else:
won = True
return
To fix your marking method, take input inside your method markit()
You can't use try-catch here because python lists are negative indexed as well.
Use if conditions to ensure row/col is within the range.
Below is a fix.
def markit(mark):
while True:
row=int(input("P1||Enter row:"))
col=int(input("P1||Enter col:"))
if row <= 3 and row >= 1 and col <= 3 and col >= 1:
if board[row-1][col-1]!='-':
print("Already Marked!!")
else:
board[row-1][col-1]=mark
break

While Loop complication in guess the number game

So I am making a guess the number game in Python 3
After the whole process, I want my while loop to generate another number so that I can start the game again without running the program again.
Please let me know what I'm doing wrong, and if you could provide me with some insight on how to use the while loop, it'll be much appreciated.
Here's the code :
import random
while True:
number = random.randint(1, 1000)
count = 0
guessed = input("Enter the number you guessed: ")
count += 1
if int(guessed) < number:
print("You guessed too low")
elif int(guessed) > number:
print("You guessed too high")
elif int(guessed) == number:
print(f'You guessed right and it took you {count} guesses to the right number which is {number}')
Not sure if the code you pasted was a typo (on indentation), but I may have accidentally changed your implementation.
Regardless, you should just add another while loop, and a break condition when the user gets it right.
import random
while True:
number = random.randint(1, 1000)
count = 0
while True: # Added, and added another layer of indentation
guessed = input("Enter the number you guessed: ")
count += 1
if int(guessed) < number:
print("You guessed too low")
elif int(guessed) > number:
print("You guessed too high")
elif int(guessed) == number:
print(f'You guessed right and it took you {count} guesses to the right number which is {number}')
break # Added
In doing so, the code will keep looping to guess the correct number until they are correct. And then generate a new number to guess. However this code will never end unless you add another breaking condition (such as setting a flag the while loop will check to break out of the outer loop.
I wrote some quick code that prompts the user if they want to continue playing or not and then loops through and continues the game with a new number if they want to. I also fixed some minor bugs. Your count kept getting reset in the loop so it would always say you found it in 1 try.
import random
def promptUser():
user_continue = input("Do you want to continue playing? y/n")
if (user_continue == 'y'):
number = random.randint(1, 10)
game(0, number)
def game(count, number):
while True:
guessed = input("Enter the number you guessed: ")
count += 1
if int(guessed) < number:
print("You guessed too low")
elif int(guessed) > number:
print("You guessed too high")
elif int(guessed) == number:
print(f'You guessed right and it took you {count} guesses to the right number which is {number}')
promptUser()
break
promptUser()

Resources