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()
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
For my intro to programming class, I need to create a program that randomly shuffles a deck of cards and then outputs the rank & suit (shown as a unicode symbol) in a 4x13 grid. What I have so far is below. How do I get it to give a random output? It currently outputs the cards in order by rank and suit. How do I get it to output in a 4x13 grid? It currently outputs in a 13x4 grid.
Here's an example of what my output is supposed to look like:
example output
(For the class, my prof wanted us to list both the separate tuples & nested sequence which is why they're both there, sorry if it makes the code appear messy)
import random
#Cards
SUITS = "\u2663","\u2665","\u2666","\u2660"
PIPS = "A","2","3","4","5","6","7","8","9","10","J","Q","K"
deck = [("A","\u2663"),("2","\u2663"),("3","\u2663"),("4","\u2663"),
("5","\u2663"),("6","\u2663"),("7","\u2663"),("8","\u2663"),("9","\u2663"),
("10","\u2663"),("J","\u2663"),("Q","\u2663"),("K","\u2663"),("A","\u2665"),
("2","\u2665"),("3","\u2665"),("4","\u2665"),("5","\u2665"),("6","\u2665"),
("7","\u2665"),("8","\u2665"),("9","\u2665"),("10","\u2665"),("J","\u2665"),
("Q","\u2665"),("K","\u2665"),("A","\u2666"),("2","\u2666"),("3","\u2666"),
("4","\u2666"),("5","\u2666"),("6","\u2666"),("7","\u2666"),("8","\u2666"),
("9","\u2666"),("10","\u2666"),("J","\u2666"),("Q","\u2666"),("K","\u2666"),
("A","\u2660"),("2","\u2660"),("3","\u2660"),("4","\u2660"),("5","\u2660"),
("6","\u2660"),("7","\u2660"),("8","\u2660"),("9","\u2660"),("10","\u2660"),
("J","\u2660"),("Q","\u2660"),("K","\u2660")]
#Retrieve random card
def deal_card():
for suit in SUITS:
for pip in PIPS:
print(suit + pip,end=" ")
print()
#Main Portion
deal_card()
from itertools import product
from random import shuffle
SUITS = ["\u2663","\u2665","\u2666","\u2660"]
PIPS = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
deck = list(product(PIPS, SUITS))
shuffle(deck)
Then put in your print logic. Here is a fairly lazy print method that accomplishes what your example link shows:
for i in range(0, len(deck), 4):
print("{} {} {} {}".format(deck[i][0]+deck[i][1],deck[i+1][0]+deck[i+1][1],deck[i+2][0]+deck[i+2][1],deck[i+3][0]+deck[i+3][1]))
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
Can't seem to find an answer to this;
For example:
health -= random.randrange(0, 5)
How would I print out (edit: to the the console) what number was selected in the range to show how much health was deducted?
It depends where you want to print it... In addition i hope that you initialized health variable to something and then use -= to decrease it... If you just want to print it to console you have to
print health #python 2.x
or
print(health) #python 3.x
of course you have to store the random number was selected
rand = Random.randrange(0,5)
print rand
What about this:
rand = random.randrange(0, 5)
health -= rand
print rand
if you are on python interpreter command, just type health. It will print it out. If you are doing through code, do
rand = random.randrange(0, 5)
print rand
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
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.