I'm new to Computer Science and to Stack Overflow and I'm currently stuck with this IGCSE summer work. I'm creating a game called "100" in Python and the rule is whoever reaches 100 first wins. The coding's below:
# Exercise 4 - Beginner
while True:
print("~~~~~ THE 100 GAME - CODED BY SASU ~~~~~")
print('----- MENU -----')
print('1 - Start multiplayer game')
print('2 - Quit game')
choice = int(input('What are you going to do? Input the number representing your choice.'))
if choice == 1:
print('Let's do it!')
elif choice == 2:
print('See you soon.')
break # In order to stop the program and quit
else:
print('What the heck? Type again.')
print("~~~~~ THE 100 GAME - CODED BY SASU ~~~~~")
print('----- MENU -----')
print('1 - Start multiplayer game')
print('2 - Quit game')
choice = int(input('What are you going to do? Input the number representing your choice.'))
if choice == 1:
print('Let's do it!')
elif choice == 2:
print('See you soon.')
break # In order to stop the program and quit
else:
print('What the heck? Type again.') # After this it just starts the multiplayer game right away I don't know how to fix this
while True:
print('Yooo player 1, tell me your name:')
oneName = input()
print('Well then, player 2, what about ya?')
twoName = input()
print('Well hey there, ' + oneName + ' and ' + twoName + ', and welcome to 100!')
print('You will each take turns to choose a number between 1 and 10.')
print('The first person to reach 100 is the winner.')
print("Have fun, and let's get started!")
totalNumber = 0
while True:
while totalNumber < 100:
numberOne = input(oneName + ', give me a number between 1 and 10.') # I don't know how to limit the range though
numberOne = int(numberOne)
totalNumber += numberOne
print("Got it, " + oneName + "! Below is the total right now:")
print(totalNumber)
break
while totalNumber < 100:
numberTwo = input(twoName + ', give me a number between 1 and 10.')
numberTwo = int(numberTwo)
totalNumber += numberTwo
print("Got it, " + twoName + "! Below is the total right now:")
print(totalNumber)
break
After all these codes, when we run the game, players can input numbers until the total reaches the number 100. However, I don't know how to make the game announce the winner. Like, keep track of the players' progress. If anyone can help, that would be great! Also, if you can help me out in fixing my problems in the code, that would be a huge help! Thank you :)
Just a hint. Use dict in python.
for example :
d = {'one' = 0,'two' = 0}
Increment key value each time and check if it reaches to 100.
Hope you get it.
Related
I'm making a guess the number game. My code is almost complete but I need to make it so that the program asks the player if they want to play again and then restarts. Could someone help me with how I should go about that? I tried making a new function ex. def game_play_again and then call the game_play() function but it's not reseting the attempts which leads to it not looping correctly.
This is my code right now
import random
MIN = 1
MAX = 100
attempts = 5
win = False
number = random.randint(MIN,MAX)
last_hint = f"{'EVEN' if number%2 == 0 else 'ODD'}"
#print game instructions
def game_start():
print(f"Im thinking of a number between {MIN} and {MAX}. Can you guess it within
{attempts} attempts? ")
input("Press enter to start the game ")
#process user input
def game_play():
global number, attempts, last_hint, win
while attempts > 0:
print()
print(f"You have {attempts} {'attempts' if attempts > 1 else 'attempt'} left.")
if attempts == 1:
print(f"This is your last chance. So i'll give you one more hint. Its's an {last_hint} number.")
while True:
try:
guess = int(input("Try a lucky number: "))
if guess in range(MIN, MAX+1):
break
else:
print(f"Please enter numbers between {MIN} and {MAX} only!")
except ValueError:
print("Plese enter numbers only!")
if guess == number:
win = True
break
if attempts == 1:
break
if guess > number:
if guess-number > 5:
print("Your guess is too high. Try something lower.")
else:
print("Come on you are very close. Just a bit lower.")
else:
if number-guess > 5:
print("Your guess is too low. Try something higher.")
else:
print("Come on you are very close. Just a bit higher.")
attempts -= 1
#print game results
def game_finish(win):
if win:
print("Congratulations you guessed it!")
else:
print(f"The number I was thinking of is {number}. Sorry you lost. Better luck next time!")
game_start()
game_play()
game_finish(win)
You can simply reset your variables to initial values and then call game_play()
def game_finish(win):
if win:
print("Congratulations you guessed it!")
else:
print(f"The number I was thinking of is {number}. Sorry you lost. Better luck next time!")
want_play_again = int(input("Want play again? [1-Yes / 2-No]"))
if want_play_again == 1:
game_play_again()
else:
print("Bye")
/
def game_play_again():
attempts = 0
win = False
game_play()
Within a while(True) loop, write a menu driven statement asking the user if they want to repeat. If they do, initialise values and call game methods. If they do not, break the loop.
pseudocode:
while(True):
choice = input('play again? y/n)
if choice=='y':
attempts, win = 5, False
number = random.randint(MIN,MAX)
last_hint = f"{'EVEN' if number%2 == 0 else 'ODD'}"
game_start()
game_play()
game_finish()
elif choice=='n':
break
else:
print('invalid input')
The above code should be in main, with all methods within its scope.
Better yet, in place of all the initializations, add an init() method declaring them and call them when necessary.
The indentation in the code you have submitted is faulty, so I'm not sure if a related error is involved.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am making a text-based adventure game, however i tried running part of my program and there is an indentation error in line where there are astricks in which i cannot fix. Could someone give me some pointers. Thanks! All help will be well appreciated
Here is apart of my code:
import random
import time
randint = random.randint(1, 9)
randint2 = random.randint(1, 8)
randint3 = random.randint(1, 7)
randint4 = random.randint(1, 3)
person_name = input('What is your name? ')
print('your name is: ',person_name)
print('you have been trapped in a haunted house....You have to hurry and escape this house')
time.sleep(0.9)
roomchoice_1 = input('You are currently located in the living room. Would you like to walk to the Kitchen, bedroom, or the basement?: ')
time.sleep(1.32)
if roomchoice_1 == 'Kitchen':
print('okay you are now in the kitchen, now solve the combination lock. It will help you get out of here!')
time.sleep(1.5)
print('You have three guesses, if you get all three wrong......well....you,ll die. Good Luck!')
time.sleep(1.5)
num1 = int(input('enter your first number, from 1 - 9'))
print(' the correct number is: ' + randint)
if num1 == randint:
print('correct! You may now move on')
else:
print('oops, that wasnt the correct number. Try again. You now have 2 tries remaining')
num2 = int(input('Choose your second number, from 1 - 8'))
if num2 == randint2:
print('Congrats you have successfully found the code')
else:
print('uhhh....that wasnt the correct number either...try again.')
num3 = int(input('choose your third and final number between 1 and 7'))
print('the correct number is', randint3)
if num3 == randint3:
print('well done ,success, you have opened the combination lock... You have obtained a key')
L_1 = input(' Now would you like to travel to the bedroom or basement')
if L_1 == 'Bedroom':
#insert bedroom code
***else:***
print('Oh NO!, you have guessed incorrectly three times....')
print(pass)# insert in print statement a code photo of a hole
print(person_name, ' has died')
print(pass)# insert text image of skull
if roomchoice_1 == 'bedroom':
print('You have walked into the bedroom.....')
time.sleep(1)
print('LOOK... there are three doors... You have to choose the correct one...carefull! You only have one chance')
choice_door = int(input('Choose one of the following doors: Door 1, Door 2, or Door 3'))
if choice_door = randint4:
print('Good Job... You have chosen the right door....')
time.sleep(1)
print('look behind the door... there is a chest...try open it...')
open_chest = input('Would you like to open the chest')
if open_chest == 'yes':
print('Look... there is another key... this might be the key that opens the from')
So it is highly recommended to ident your code with 'TAB' to avoid those mistakes, so if you have any thing inside you 'if' statement it should be with one TAB and so one. Always use the TAB to ident. So based on that, here it is.
import random
import time
randint = random.randint(1, 9)
randint2 = random.randint(1, 8)
randint3 = random.randint(1, 7)
randint4 = random.randint(1, 3)
person_name = input('What is your name? ')
print('your name is: ',person_name)
print('you have been trapped in a haunted house....You have to hurry and escape this house')
time.sleep(0.9)
roomchoice_1 = input('You are currently located in the living room. Would you like to walk to the Kitchen, bedroom, or the basement?: ')
time.sleep(1.32)
if roomchoice_1 == 'Kitchen':
print('okay you are now in the kitchen, now solve the combination lock. It will help you get out of here!')
time.sleep(1.5)
print('You have three guesses, if you get all three wrong......well....you,ll die. Good Luck!')
time.sleep(1.5)
num1 = int(input('enter your first number, from 1 - 9'))
print(' the correct number is: ' + randint)
if num1 == randint:
print('correct! You may now move on')
else:
print('oops, that wasnt the correct number. Try again. You now have 2 tries remaining')
num2 = int(input('Choose your second number, from 1 - 8'))
if num2 == randint2:
print('Congrats you have successfully found the code')
else:
print('uhhh....that wasnt the correct number either...try again.')
num3 = int(input('choose your third and final number between 1 and 7'))
print('the correct number is', randint3)
if (num3 == randint3):
print('well done ,success, you have opened the combination lock... You have obtained a key')
L_1 = input(' Now would you like to travel to the bedroom or basement')
if (L_1 == 'Bedroom'):
pass #insert bedroom code
else:
print('Oh NO!, you have guessed incorrectly three times....')
#print(pass) # insert in print statement a code photo of a hole
print(person_name, ' has died')
#print(pass)# insert text image of skull
if roomchoice_1 == 'bedroom':
print('You have walked into the bedroom.....')
time.sleep(1)
print('LOOK... there are three doors... You have to choose the correct one...carefull! You only have one chance')
choice_door = int(input('Choose one of the following doors: Door 1, Door 2, or Door 3'))
if choice_door == randint4:
print('Good Job... You have chosen the right door....')
time.sleep(1)
print('look behind the door... there is a chest...try open it...')
open_chest = input('Would you like to open the chest')
if open_chest == 'yes':
print('Look... there is another key... this might be the key that opens the from')
i have to make this GUESS THE NUMBER Gamme from 1-100 that will restart if user wants to play again,
the user can try to find the number 10 times.
But i have a problem..
every time the user says "yes" to play again,the program will not change the random number,i try to find some solution but i didnt
here is the code
import random
guesses = 0 # μετραει ποσες προσπαθειεςς εγιναν απο τον χρηστη
print("Hello,lets play a game...and try to find the number i have guess!!")
number = random.randint(1, 100)
**while guesses < 11:
print('Please Guess a number from (1-100):')
enter = input()
enter = int(enter)
guesses = guesses + 1
if enter < number:
print('This number you enter is lower,please try again')
if enter > number:
print('This number you enter is higher,please try again')
if enter == number:
score = 10 - guesses
score = str(score)
guesses = str(guesses)
print('Well Done, You found it! \nYor Score is' + score + '!')
print('DO you want to play again; yes/no:')
out = input()
if out == "no":
break
elif out == "yes":
guesses = 0
if guesses > 10:
number = str(number)
print("i'm sorry you lost, the number is " + number)
print("Have a great time")**
In addition to reset the guesses inside the elif out == "yes" block, reset also the number. Try:
elif out == "yes":
guesses = 0
number = random.randint(1, 100)
My program has errors and bugs that I need to fix. It's about a "robot" that is a waiter in a restaurant. I am still a beginner.
I tried looking through my program for the bugs and tried indenting and dedenting different parts of my program to try and get it working. I als o tried messing around with the operators but nothing seems to work.
import sys, time
total = 0
menu = ["0 - Swedish Meatballs - 5$", "1 - Japanese Sushi - 7$", "2 - Indian Rice - 9$", "3 - Quit"]
price = [5,7,9,0]
print("Hello welcome to the restaurant! I am your robotic waiter")
action = int(input("What would you like to do? \n 1. Look at the menu \n 2. Order \n 3. Take more time \n 4. Ask for the total \n 5. Exit \n Please enter a number here: "))
while action != 5:
if action == 1:
print(menu)
action = int(input("What would you like to do? \n 1. Look at the menu \n 2. Order \n 3. Take more time \n 4. Ask for the total \n 5. Exit \n Please enter a number here: "))
elif action == 2:
print(menu)
food = int(input("What would you like? "))
while food != 3:
priceoffood = price[food]
total = total + priceoffood
if food != 3:
more = input("More things? Reply with y or n: ")
if more == "y":
print(menu)
food = int(input("What would you like? "))
if priceoffood != 3:
print(food)
print(price[food])
priceoffood = price[food]
total = total + priceoffood
else:
break
elif action == 3:
time = int(input("How many minutes more do you need? "))
while int(time) > 30:
print ("Isn't that too long? ")
time = input("How many minutes more do you need? ")
print("Okay, ill be back when your " + str(time) + " minutes are over!")
time.sleep(time*60)
elif action == 4:
print("Your total is: " + str(total))
quit()
I would like the menu functions to be working just like how they are expected to be.
You imported time, but also used it as a variable. Change your variable to something other than time. For example:
timer = int(input("How many minutes more do you need? "))
while int(timer) > 30:
print ("Isn't that too long? ")
timer = input("How many minutes more do you need? ")
print("Okay, ill be back when your " + str(timer) + " minutes are over!")
time.sleep(timer*60)
I'm trying to break the loop by saying
if deff <= int(total):
break
but the loop will break regardless of the input being negative or more than the total it will break the loop
any advice of what am i doing wrong?
P.S i know i will new a formula to decide if player won or lost. right now im just trying to figure out the first code before going there
first time on programming and teacher not helpful ):
def intro():
greeting()
print('How much money do you want to start with?')
print('Enter the starting amount of dollars.', end='')
total = int(input())
print('Your current total is '+ str(total)+'\n')
while True:
print('How much money do you want to bet (enter 0 to quit)?', end='');
# Bett will be the amount of money that the player will play with
bett = int(input())
if bett > int(total):
print('ERROR You don\'t have that much left')
if bett < int(0):
print('ERROR: Invalid bet amount\n')
if bett <= int(total)
break
# Function shows results of slot machine after handle being pulled
def random():
import random
num1 = random.randint(1, 5)
num2 = random.randint(1, 5)
num3 = random.randint(1, 5)
print('/---+---+---\ ')
print('|-'+ str (num1)+'-|-'+ str(num2) +'-|-'+ str (num3) +'-|')
print('\---+---+---/ ')
intro()
You need to use elif and else in the successive conditional tests:
bett = int(input())
if bett > total:
print('ERROR You don\'t have that much left')
elif bett < 0:
print('ERROR: Invalid bet amount\n')
else:
break
That way, only one of the statements in executed, instead of more or more.
NB:
It's not necessary to use theint() constructor all the time on something that is already an int
In this block:
if bett <= int(total)
break
You have a syntax error. Add a colon to the end of hte first line:
if bett <= int(total):
break
If it were me, I would use a while loop.
play = True
while play == True:
#all the code
#to be executed
#indented here
#
#Ask user if they want to continue playing
p = input("Play again?[y/n] ")
playAgain = str(p)
if playAgain == "y":
play = True
else:
play = False