Implementation of simple game with many rounds and players [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have to make a game where n people play hangman and the game keeps their results and if they win the round, then they go on until there is only one player left(that player wins) or none left(nobody wins). My hangman code is ready, so I do not need anything concerning the hangman function. However, I need some help to make a function that does the following:
1)Asks how many players will play
2)Asks their names and stores them
3)Plays the first round and if hangman()==True (meaning the player won) for a player, then this player goes on to the next round, otherwise not
4)If somebody wins, then we have a winner and the game ends
I have already made the part that the game asks the number of player, asks their names and makes them play. My hangman() function returns either True or False. However, it seems that I have a problem. Every time a player plays the game, the hangman() function runs twice. I don't know why this happens. I would like some help fixing that and also to write the part where each round is played.
def game():
players_dict={}
results=[]
num_of_players=int(input('How many players will play? '))
for i in range(1,num_of_players+1):
a=input('Give the name of Player {}: '.format(i))
players_dict['Player {}'.format(i)]=a
for i in range(1,num_of_players+1):
print(players_dict['Player {}'.format(i)])
hangman()
if hangman()==False:
results+=False
else:
results+=True

All you need to do is get rid of the first hangman() call:
...
for i in range(1, num_of_players + 1):
print(players_dict['Player {}'.format(i)]
if hangman() is False:
results += False
else:
results += True
If you need to keep hold of the value returned, assign it to a variable beforehand:
...
r = hangman()
if r is False:
results += False
else:
results += True
Furthermore, you can shorten this code simply by writing the following (assuming you don’t keep the result):
results += hangman()

Related

Antonia and David are playing a game. Each player starts with 100 points [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Question: Antonia and David are playing a game. Each player starts with 100 points. The game uses standard six-sided dice and is played in rounds. During one round, each player rolls one die. The player with the lower roll loses the number of points shown on the higher die. If both players roll the same number, no points are lost by either player. Write a program to output the final scores and the winner
Input Specification The first line of input contains the integer n (1 ≤ n ≤ 15), which is the number of rounds that will be played. On each of the next n lines, will be two integers: the roll of Antonia for that round, followed by a space, followed by the roll of David for that round. Each roll will be an integer between 1 and 6 (inclusive). Output Specification The output will consist of two lines. On the first line, output the number of points that Antonia has after all rounds have been played. On the second line, output the number of points that David has after all rounds have been played.
One of my many problems is making the program list the correct number of inputs the first input specifies.
Here is what I have so far:
I know I only asked for one thing specifically, but can anyone complete this challenge so I can see what I can add to my program
Because it is a homework question, you really should try to it yourself first. With this being said, I will give you hints but I will not give you a full working program - I hope you can understand my reasoning for this.
To start, this problem definitely calls for some type of iteration as rolling a dice for n amount of times is repetitive. Whether you choose a for loop or a while loop is up to you - in this example I use a while loop. After getting the amount of rounds (don't forget to convert the user input into int), you can write something like this:
while rounds > 0:
# simulate rolling here
rounds -= 1
Rolling a dice is a random action - there is a 1/n chance to roll a number where n is the number of sides on the dice. I would suggest creating a list of all possibilities:
dice = [1,2,3,4,5,6]
And then use choice() from the random module to select a random item from this list.
from random import choice
david_roll = choice(dice)
antonia_roll = choice(dice)
Now that you have the values of each roll, you can just perform some simple comparison on the rolls:
if david_roll > antonia_roll:
# modify scores accordingly
elif david_roll < antonia_roll:
# modify scores accordingly

How can I solve this problem without using lists? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Problem: You're a swimmer, and you want to compare all of your race times to find the fastest one. Write a program that continuously takes race times as doubles from standard input, until the input is "no more races," at which point it should print out the time of your fastest race.
The answer I found on another thread was using lists like:
list1 = []
race_time = input("Enter a race time or type no more races to quit: ")
while race_time != "no more races":
list1.append(float(race_time))
race_time = input("Enter a race time or type no more races to quit ")
print(min(list1))
The problem is from if-else, and loops chapter and lists aren't introduced to us yet. How would I store the race times and compare them?
You can just store the best time that you've seen so far and whenever the new time is better overwrite the old one:
import math
best_time = math.inf
[... your code ...]
while ...:
if race_time < best_time:
best_time = race_time
Without list, it will be a little bit difficult to store all races. But as you are only looking for the speedest race:
race_time = input("Enter a race time or type no more races to quit: ")
best_time=float(race_time)
while race_time != "no more races":
race_time = input("Enter a race time or type no more races to quit ")
if race_time != "no more races":
race_time=float(race_time)
if race_time<best_time:
best_time=race_time
print(best_time)

How to create combat system in python [closed]

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 8 years ago.
Improve this question
Last line doesn't work and I wanna know how to give the Goblin random damage between 1-5.
my_character = {'name': '','health': 30}
print "You take a step back red eyes get closer..."
print "A goblin holding small dagger appears"
enemy = {'name':'Goblin','health':10}
print enemy
print "Goblin attacks you..."
my_character['health'] - 10
To choose a random number, you can use import randint fromm the random module.
To get a number between one and five use code like this:
from random import randint
goblin_damage = randint(1,5)
This will generate a random number between one and five.
To remove this amount of damage from player['health'] you can use player['health'] -= goblin_damage.
If you are wondering why my_character['health'] is not changed, the reason is simply that you never assign to it. Try
my_character['health'] = my_character['health'] - 10
or, the slighter shorter
my_character['health'] -= 10
If your question is something else, then please clarify the question.

Write a Python program that repeatedly asks the user to input coin values until the total amount matches a target value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
However, I realized that I don't actually know how to do this myself without examining every possible combination of coins. There has to be a better way of solving this problem, but I don't know what the generic name for this type of algorithm would be called, and I can't figure out a way to simplify it beyond looking at every solution.
I was wondering if anybody could point me in the right direction, or offer up an algorithm that's more efficient.
You can try something like this:
MaxAmount = 100
TotalAmount = 0
while TotalAmount < MaxAmount:
#Here if you want it to be more precise on decimals change int(raw_input("Amount: ")) to float(raw_input("Amount: "))
EnteredAmount = float(raw_input("Amount: "))
if EnteredAmount > MaxAmount:
print "You can not go over 100"
elif TotalAmount > MaxAmount:
#You can go two ways here either just set TotalAmount to MaxAmount or just cancel the input
print "You can not go over 100"
elif EnteredAmount <= MaxAmount:
TotalAmount = TotalAmount + EnteredAmount
print TotalAmount
print "You have reached the total amount of ", MaxAmount
Could use a loop into an if - elif - else statements
e.g. populate a variable with your amount, then using this for the loop condition keep asking to take away coin amounts until you reach 0

Python Help Again [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am using Python 3.2 Just so you know what I am doing, here is the assignment:
~The function random.randint from the random module can be used to produce an integer from a range of values. For example, random.randint(1,6) produces the values 1 to 6 with equal probability. A simple tutoring program may randomly select two numbers between 0 and 12 and ask a question such as the following: What is 6 times 8? Upon receiving user response, the computer checks to see if the answer is correct and gives encouraging remarks. Write a program that will loop 10 times producing questions of this form and gives a score for the user at the end.
Here is what I have in my program:
print ("Hello. Let's begin")
for i in range (1,10):
from random import randint
x=randint (0,12)
y=randint (0,12)
print (x,"*" y,"=?")
product= int(input ("What is the product?")
if (product==x*y):
print ("Awesome! That is correct!")
else:
print ("Sorry, that is not correct, but let's try another one!")
I have everything working with all of this. It asks the user a random multiplication question and responds ten times. What I do not understand how to do is to give the user a score at the end. I'm brainstorming ideas and not much is really working. I think I would have to do something like:
score=
But I don't know how to tell the program to calculate the number of correct answers... Do I say score=number of if?
And then when I print the score I can just say:
if (score>5) :
print: ("Great job! You scored a",score,"out of ten!")
else:
print: ("Not the best score, but you can try again! You scored a",score,"out of ten.")
Or is there maybe an easier way to do this?
It seems like it would be simplest to just make a new variable ("score" or suchlike) and initialize it as 0 before the loop. Then, when you check if a user was correct, just increment it by one if it was right, and leave it alone if it was wrong.
Hope this helps!
First, set score to 0
score = 0
then in the loop, try something like
if (product==x*y):
print ("Awesome! That is correct!")
score += 1
else:
print ("Sorry, that is not correct, but let's try another one!")
the important part being the score += 1 this increases the score by one when you get a correct answer. You can the put your score > 5 in after the loop.

Categories