It's me again, from like an hour ago.
So I was messing around with some of the answers that I was provided with and well this is what I got now:
import random
while != "No":
print("Would you like to get your set?")
choice = input("Yes or No: ")
if choice == 'Yes:
print(swimming())
else:
break
def swimming():
def stroke():
x = random.randint(1, 150)
if x < 51:
print("Freestyle")
elif x >51 and x<100:
print("Breaststroke")
else:
print("Butterfly")
def laps():
i = random.randint(1, 300)
if i <= 99:
print("Ten Laps")
elif i >=100 and i <= 199:
print("Fifteen Laps")
else:
print("Twenty Laps")
def style():
j = random.randint(0,15)
if j < 5:
print("Paddles")
elif j > 6 and j < 10:
print("Bouy")
else:
print("Kick-Board")
I currently get an error in my command line saying that "!=" is an invalid syntax and can not be run.
Would this code at all work or is there something I can do to fix it?
Sorry about my code, I barely started to learn it a week ago.
while != "No":
is an incomplete condition, you need to change it to something like:
while True: # so the loop only ends/breaks on a condition inside it
Also, You have a missing ' in the line:
if choice == 'Yes:
Change it to:
if choice == 'Yes':
EDIT:
you need to define the swimming() method before the loop:
import random
def swimming():
def stroke():
x = random.randint(1, 150)
if x < 51:
print("Freestyle")
elif x >51 and x<100:
print("Breaststroke")
else:
print("Butterfly")
def laps():
i = random.randint(1, 300)
if i <= 99:
print("Ten Laps")
elif i >=100 and i <= 199:
print("Fifteen Laps")
else:
print("Twenty Laps")
def style():
j = random.randint(0,15)
if j < 5:
print("Paddles")
elif j > 6 and j < 10:
print("Bouy")
else:
print("Kick-Board")
while True:
print("Would you like to get your set?")
choice = input("Yes or No: ")
if choice == 'Yes':
print(swimming())
else:
break
Also, this would return None as you are not calling any method inside the swimming() method or returning anything.
So you can call the methods inside like:
def swimming():
def stroke():
x = random.randint(1, 150)
if x < 51:
print("Freestyle")
elif x >51 and x<100:
print("Breaststroke")
else:
print("Butterfly")
def laps():
i = random.randint(1, 300)
if i <= 99:
print("Ten Laps")
elif i >=100 and i <= 199:
print("Fifteen Laps")
else:
print("Twenty Laps")
def style():
j = random.randint(0,15)
if j < 5:
print("Paddles")
elif j > 6 and j < 10:
print("Bouy")
else:
print("Kick-Board")
stroke()
laps()
style()
Is this what you want?
import random
def swimming():
def stroke():
x = random.randint(1, 150)
if x < 51:
print("Freestyle")
elif x >51 and x<100:
print("Breaststroke")
else:
print("Butterfly")
def laps():
i = random.randint(1, 300)
if i <= 99:
print("Ten Laps")
elif i >=100 and i <= 199:
print("Fifteen Laps")
else:
print("Twenty Laps")
def style():
j = random.randint(0,15)
if j < 5:
print("Paddles")
elif j > 6 and j < 10:
print("Bouy")
else:
print("Kick-Board")
stroke()
laps()
style()
while True:
print("Would you like to get your set?")
choice = input("Yes or No: ")
if choice == 'Yes':
swimming()
else:
break
Related
Hope you can help me. Please see the code below. What I am trying to achieve is for code to stop when "x" or "y" gets to 0. At the moment, "x" gets to 0 first, but function_b() is still run after that. I would like that if "x" gets to 0, function_b() would not run afterwards. How do I do that?
Thank you
from random import randint
x = 10
y = 10
def roll_dice():
random_dice = randint(1, 6)
return random_dice
def function_a():
global x
while x > 0:
dice = roll_dice()
if dice >= 1:
x -= 5
else:
print('Better luck next time')
if x <= 0:
break
else:
function_b()
def function_b():
global y
while y > 0:
dice = roll_dice()
if dice >= 1:
y -= 5
else:
print('Better luck next time')
if y <= 0:
break
else:
function_a()
def turns():
if x > 0:
function_a()
else:
print('good game')
if y > 0:
function_b()
else:
print('good game')
turns()
I made some adjustement on your code :
from random import randint
x = 10
y = 10
def roll_dice():
random_dice = randint(1, 6)
return random_dice
def function_a():
global x
while x > 0 and y != 0:
dice = roll_dice()
if dice >= 1:
x -= 5
else:
print('Better luck next time')
if x <= 0:
break
else:
function_b()
def function_b():
global y
while y > 0 and x !=0:
dice = roll_dice()
if dice >= 1:
y -= 5
else:
print('Better luck next time')
if y <= 0:
break
else:
function_a()
def turns():
if x > 0:
function_a()
else:
print('good game')
if y > 0:
function_b()
else:
print('good game')
turns()
Heres the copy of my code :) this game is part of my personal activity.
so after purchasing a clue (from: def game_hints) i want to return to def Game_process.
import random
SCORE = 0
ROUNDS = 1
def player_stats():
print(f"SCORE: {SCORE} | ROUNDS: {ROUNDS}")
def game_hints(USER_GUESS, Mystery_NUM):
print("Would you like purchase a hint for 5 points? [1/2]: ")
USER_HINT = int(input())
global SCORE
if USER_HINT == 1:
SCORE= SCORE - 5
if USER_GUESS > Mystery_NUM and Mystery_NUM % 2 == 0:
print("Mystery Num is even and try a smaller guess")
elif USER_GUESS > Mystery_NUM and Mystery_NUM % 2 == 1:
print("Mystery Num is odd and try a smaller guess")
elif USER_GUESS < Mystery_NUM and Mystery_NUM % 2 == 0:
print("Secret Num is even and try a larger guess")
elif USER_GUESS < Mystery_NUM and Mystery_NUM % 2 == 1:
print("Mystery Num is odd and try a larger guess")
def Game_Process():
global ROUNDS
while True:
if ROUNDS <= 10:
Mystery_NUM = random.randrange(10)
print(Mystery_NUM) #remove before final product
print("Guess the num [1-10]: ")
USER_GUESS = int(input())
if USER_GUESS == Mystery_NUM:
print("\nGood Job! +5 Coins!")
global SCORE
SCORE = SCORE + 10
ROUNDS += 1
player_stats()
else:
print("Wrong! Try Again")
game_hints(USER_GUESS, Mystery_NUM)
else:
print("Game Over!")
Game()
def Game():
user_opt = input("\"Welcome to Guess Game\" \nPress [Y] to Play or [N] to Exit: ").lower()
if user_opt == "n":
print("Good bye!")
exit()
elif user_opt == "y":
Game_Process()
else:
print("Invalid Input! [1/2]")
Game()
Game()
As shown below, this is the fuction for the hints. I was able to call this function but the only problem is that the after this fuction is done, it changes the Myster_Num.
def game_hints(USER_GUESS, Mystery_NUM):
print("Would you like purchase a hint for 5 points? [1/2]: ")
USER_HINT = int(input())
global SCORE
if USER_HINT == 1:
SCORE= SCORE - 5
if USER_GUESS > Mystery_NUM and Mystery_NUM % 2 == 0:
print("Secret Num is even and try a smaller guess")
elif USER_GUESS > Mystery_NUM and Mystery_NUM % 2 == 1:
print("Secret Num is odd and try a smaller guess")
elif USER_GUESS < Mystery_NUM and Mystery_NUM % 2 == 0:
print("Secret Num is even and try a larger guess")
elif USER_GUESS < Mystery_NUM and Mystery_NUM % 2 == 1:
print("Mystery Num is odd and try a larger guess")
First, you must remove the else statement in the game_hints function because it restarts a full GameProcess and therefore indeed recompute a mysterious number.
Then, when you exit game_hints and come back to GameProcess, you must not come back to the big loop because it will indeed recompute a mysterious number. The solution is to have an inner loop inside each round that you exit only if the player guessed the correct value using the break keyword.
def Game_Process():
SCORE = 0
ROUNDS = 1
while True:
if ROUNDS <= 10:
Mystery_NUM = random.randrange(10)
print(Mystery_NUM) # remove before final product
while True:
print("Guess the num [1-10]: ")
USER_GUESS = int(input())
if USER_GUESS == Mystery_NUM:
print("\nGood Job! +5 Coins!")
SCORE = SCORE + 10
ROUNDS += 1
player_stats()
break
else:
print("Wrong! Try Again")
game_hints(USER_GUESS, Mystery_NUM)
else:
print("Game Over!")
Game()
It just skips over the fact that I put no when it asks I put no, and when I do put no, it just re-asks me if I want to hit.
I've tried making a new while statement for when you don't want to hit and it still didn't work.
import time
from random import randint
cardg = 0
cardg1 = 0
yeet = 1
while cardg < 21:
hit = input("Do you want to hit? (Yes or No): ")
if hit == "yes" or hit == "Yes":
num = randint(1, 10)
num2 = randint(1, 4)
card = num
card2 = num2
if num == 1:
card1 = "Ace"
elif num == 11:
card1 = "Jack"
elif num == 12:
card1 = "Queen"
elif num == 13:
card1 = "King"
if num2 == 1:
card2 = "Hearts"
if num2 == 2:
card2 = "Diamonds"
if num2 == 3:
card2 = "Spades"
if num2 == 4:
card2 = "Clubs"
if num == 1:
card = input("Would you like your ace to be a 1 or 11?\nAnswer: ")
print("Ace of " + str(card2))
cardg += int(card)
time.sleep(1)
if cardg < 21:
print(str(card) + " of " + str(card2))
elif cardg == 21:
time.sleep(1)
print(str(card) + " of " + str(card2))
print("Blackjack, you win!")
yeet = 0
elif cardg > 21:
print(str(card) + " of " + str(card2))
print("Busted, you lose.")
yeet = 0
else:
while cardg1 < 21 and cardg1 > 18:
num = randint(1, 11)
cardg1 += int(num)
num2 = randint(1, 4)
card = num
card2 = num2
if num == 1:
card1 = "Ace"
elif num == 11:
card1 = "Jack"
elif num == 12:
card1 = "Queen"
elif num == 13:
card1 = "King"
if num2 == 1:
card2 = "Hearts"
if num2 == 2:
card2 = "Diamonds"
if num2 == 3:
card2 = "Spades"
if num2 == 4:
card2 = "Clubs"
cardg1 += int(card)
time.sleep(1)
if cardg1 < 21 and cardg1 > 18:
print(str(card) + " of " + str(card2))
print("You have " + str(cardg) + " points, dealer has " + str(cardg1) + " points.")
if cardg > cardg1:
print("Dealer has won!")
yeet = 0
else:
print("You have won!")
yeet = 0
elif cardg1 == 21:
time.sleep(1)
print(str(card) + " of " + str(card2))
print("Dealer has blackjack, you lose!")
yeet = 0
elif cardg1 > 21:
print(str(card) + " of " + str(card2))
print("Dealer busted, you win!")
yeet = 0
When ran, this is this output. The getting 21 and busting code works.
Do you want to hit? (Yes or No): yes
3 of Clubs
Do you want to hit? (Yes or No): yes
9 of Hearts
Do you want to hit? (Yes or No): no
Do you want to hit? (Yes or No): n
Do you want to hit? (Yes or No): no
Do you want to hit? (Yes or No):
cardg1 is initialized to 0. Therefore, the while condition cardg1 < 21 and cardg1 > 18 fails, as 0 is not in between 18 and 21, hence the else block does nothing.
I want the computer playing with the player with until one of them scores 10 points:
from graphics import *
board=[[0,0,0],[0,0,0],[0,0,0]]#as bourd
window=GraphWin("Tic Tac Toe",700,700)
L0=Line(Point(250,50),Point(250,650)).draw(window)
L1=Line(Point(450,50),Point(450,650)).draw(window)
L2=Line(Point(50,250),Point(650,250))
L2.draw(window)
L3=Line(Point(50,450),Point(650,450))
L3.draw(window)
xTurn=True
num=0
while num<9:
b=window.getMouse()
pa,pb=int((b.x-50)/200)+1,int((b.y-50)/200)+1
if board[pb-1][pa-1]==0:
num+=1
if xTurn:
tex="X"
xTurn=False
else:
tex="O"
xTurn=True
h=Text(Point(pa*150+50*(pa-1),pb*150+50*(pb-1)),tex).draw(window)
h.setSize(36)
if xTurn:
h.setFill("blue")
board[pb-1][pa-1]=1
else:
h.setFill("red")
board[pb-1][pa-1]=2
if num>4:
if (board[0][0]==1 and board[0][1]==1 and board[0][2]==1) or(board[1][0]==1 and board[1][1]==1 and board[1][2]==1) or(board[2][0]==1 and board[2][1]==1 and board[2][2]==1):
print(" O is winner")
break
elif (board[0][0]==2 and board[0][1]==2 and board[0][2]==2) or(board[1][0]==2 and board[1][1]==2 and board[1][2]==2) or (board[2][0]==2 and board[2][1]==2 and board[2][2]==2):
print(" X is winner")
break
elif (board[0][0]==2 and board[1][0]==2 and board[2][0]==2) or(board[0][1]==2 and board[1][1]==2 and board[2][1]==2) or (board[0][2]==2 and board[1][2]==2 and board[2][2]==2):
print(" X is winner")
break
elif (board[0][0]==1 and board[1][0]==1 and board[2][0]==1) or(board[0][1]==1 and board[1][1]==1 and board[2][1]==1) or (board[0][2]==1 and board[1][2]==1 and board[2][2]==1):
print(" O is winner")
break
elif (board[0][0]==1 and board[1][1]==1 and board[2][2]==1) or(board[0][2]==1 and board[1][1]==1 and board[2][0]==1):
print(" O is winner")
break
elif (board[0][0]==2 and board[1][1]==2 and board[2][2]==2) or(board[0][2]==2 and board[1][1]==2 and board[2][0]==2):
print(" X is winner")
break
if num>=9:
print("There is no winner!")
I took your program apart and put it back together again to streamline it and allow it to play ten games before quitting. It prints the results of the ten games as it exits. This isn't exactly what you are after, but I believe gives you the tools you need to do what you want:
from graphics import *
from time import sleep
PLAYERS = ['Draw', 'O', 'X']
DRAW = PLAYERS.index('Draw')
COLORS = ['black', 'blue', 'red']
EMPTY = 0
window = GraphWin("Tic Tac Toe", 700, 700)
Line(Point(250, 50), Point(250, 650)).draw(window)
Line(Point(450, 50), Point(450, 650)).draw(window)
Line(Point(50, 250), Point(650, 250)).draw(window)
Line(Point(50, 450), Point(650, 450)).draw(window)
turn = PLAYERS.index('X')
scores = [0] * len(PLAYERS)
tokens = []
while sum(scores) < 10:
for token in tokens:
token.undraw()
board = [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY]]
squares_played = 0
while squares_played < 9:
point = window.getMouse()
x, y = int((point.x - 50) / 200), int((point.y - 50) / 200)
if board[y][x] == EMPTY:
squares_played += 1
board[y][x] = turn
text = PLAYERS[turn]
token = Text(Point(200 * x + 150, 200 * y + 150), text)
token.setSize(36)
token.setFill(COLORS[turn])
token.draw(window)
tokens.append(token)
turn = len(PLAYERS) - turn
if squares_played > 4:
if EMPTY != board[0][0] == board[0][1] == board[0][2]:
print("{} is winner".format(PLAYERS[board[0][0]]))
scores[turn] += 1
break
elif EMPTY != board[1][0] == board[1][1] == board[1][2]:
print("{} is winner".format(PLAYERS[board[1][1]]))
scores[turn] += 1
break
elif EMPTY != board[2][0] == board[2][1] == board[2][2]:
print("{} is winner".format(PLAYERS[board[2][2]]))
scores[turn] += 1
break
elif EMPTY != board[0][0] == board[1][0] == board[2][0]:
print("{} is winner".format(PLAYERS[board[0][0]]))
scores[turn] += 1
break
elif EMPTY != board[0][1] == board[1][1] == board[2][1]:
print("{} is winner".format(PLAYERS[board[1][1]]))
scores[turn] += 1
break
elif EMPTY != board[0][2] == board[1][2] == board[2][2]:
print("{} is winner".format(PLAYERS[board[2][2]]))
scores[turn] += 1
break
elif EMPTY != board[0][0] == board[1][1] == board[2][2]:
print("{} is winner".format(PLAYERS[board[1][1]]))
scores[turn] += 1
break
elif EMPTY != board[0][2] == board[1][1] == board[2][0]:
print("{} is winner".format(PLAYERS[board[1][1]]))
scores[turn] += 1
break
if squares_played >= 9:
print("There is no winner!")
scores[DRAW] += 1
sleep(2)
for index, player in enumerate(PLAYERS):
print("{}: {}".format(player, scores[index]))
My rework relies on some parallel arrays, which is better than no arrays, but should evolve to a real data structure.
I am trying to code player and mob attacks for a text based RPG game I am making, I have randomint set up for player and mob hitchance and crit chance but I can't figure out how to get a new integer for them every time I restart the loop, it uses the same integer it got the first time it entered the loop.
### GAME VALUES ###
class roll_dice:
def __init__(self):
self.spawn = random.randint(1,100)
self.escape = random.randint(1,100)
self.playercrit = random.randint(1,100)
self.playerhitchance = random.randint(1,100)
self.mobcrit = random.randint(1,100)
self.mobhitchance = random.randint(1,100)
roll = roll_dice()
### ORC SPAWN ###
if fight_walk.lower() == 'fight':
orcMobSpawn()
while True:
fight_orc = input(">>> ")
if fight_orc.lower() == 'a':
### PLAYER ATTACK ###
while True:
roll.playercrit
roll.playerhitchance
if roll.playercrit <= 10 and roll.playerhitchance >= 6:
print("You crit orc for",str(userPlayer.atk * 2),"damage!")
orcMob.hp = orcMob.hp - userPlayer.atk * 2
print("Orc HP:",orcMob.hp)
break
elif roll.playercrit >= 11 and roll.playerhitchance >= 6:
print("You hit orc for",str(userPlayer.atk),"damage!")
orcMob.hp = orcMob.hp - userPlayer.atk
print("Orc HP:",orcMob.hp)
break
elif roll.playercrit >= 11 and roll.playerhitchance <= 5:
print("You missed!")
break
elif roll.playercrit <= 10 and roll.playerhitchance <= 5:
print("You missed!")
break
elif orcMob.hp <= 0 and userPlayer.hp >= 1:
print("Your HP:",str(userPlayer.hp))
print("You win!")
break
elif userPlayer.hp <= 0:
print("You died!")
exit()
### ORC ATTACK ###
while True:
roll.mobcrit
roll.mobhitchance
if orcMob.hp <= 0 and userPlayer.hp >= 1:
break
if roll.mobcrit <= 5 and roll.mobhitchance >= 25:
print("\nOrc crit for",str(orcMob.atk * 2),"damage!")
userPlayer.hp = userPlayer.hp - orcMob.atk * 2
print("Your HP:",str(userPlayer.hp))
break
elif roll.mobcrit >= 5 and roll.mobhitchance >= 25:
print("\nOrc hit for",str(orcMob.atk),"damage!")
userPlayer.hp = userPlayer.hp - orcMob.atk
print("Your HP",str(userPlayer.hp))
break
elif roll.mobcrit <= 5 and roll.mobhitchance <= 25:
print("Orc missed!")
print("Your HP:",str(userPlayer.hp))
break
elif roll.mobcrit >= 5 and roll.mobhitchance <= 25:
print("Orc missed!")
print("Your HP:",str(userPlayer.hp))
break
if orcMob.hp <= 0 and userPlayer.hp >= 1:
break
elif orcMob.hp >= 1:
continue
The problem is with your roll_dice class. You have the values defined when the class initializes, but then you never update them again. Therefore self.escape or self.spawn will always be the same value after the program starts. The easiest way to solve your problem without rewriting much would be to make another instance of roll_dice() every time you want to roll the dice. Something like:
### GAME VALUES ###
class roll_dice:
def __init__(self):
self.spawn = random.randint(1,100)
self.escape = random.randint(1,100)
self.playercrit = random.randint(1,100)
self.playerhitchance = random.randint(1,100)
self.mobcrit = random.randint(1,100)
self.mobhitchance = random.randint(1,100)
# roll = roll_dice() # you don't need to make an instance here
### ORC SPAWN ###
if fight_walk.lower() == 'fight':
orcMobSpawn()
while True:
fight_orc = input(">>> ")
if fight_orc.lower() == 'a':
### PLAYER ATTACK ###
while True:
roll = roll_dice() # make a new instance with each loop
roll.playercrit
roll.playerhitchance
if roll.playercrit <= 10 and roll.playerhitchance >= 6:
print("You crit orc for",str(userPlayer.atk * 2),"damage!")
orcMob.hp = orcMob.hp - userPlayer.atk * 2
print("Orc HP:",orcMob.hp)
break
elif roll.playercrit >= 11 and roll.playerhitchance >= 6:
print("You hit orc for",str(userPlayer.atk),"damage!")
orcMob.hp = orcMob.hp - userPlayer.atk
print("Orc HP:",orcMob.hp)
break
elif roll.playercrit >= 11 and roll.playerhitchance <= 5:
print("You missed!")
break
elif roll.playercrit <= 10 and roll.playerhitchance <= 5:
print("You missed!")
break
elif orcMob.hp <= 0 and userPlayer.hp >= 1:
print("Your HP:",str(userPlayer.hp))
print("You win!")
break
elif userPlayer.hp <= 0:
print("You died!")
exit()
### ORC ATTACK ###
Use functions like
import random
for x in range(10):
print random.randint(1,101)
use a array around for a max 100 people and generate random digits to add in your code.
You can also use a array to create random umber structure and then use the numbers to be shuffled and added as you create them
from random import *
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
shuffle(items)
print items