How to print the random.randrange number that was selected? [closed] - python

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

Related

input function : 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 5 months ago.
Improve this question
counttonum = 1
countnum = input("[•] Provide A Number :")
while counttonum < countnum:
print("[", counttonum, "] : Number = ", counttonum)
counttonum +=1
I was trying to make a counting tool that counts up to the provided number from the “input()” function.
For example:
providedNumberFromInput = 5
output = 1
2
3
4
5
And it’ll stop if the provided number is reached. Please help me.
You are very close to solution. Problem is that input() returns value as string so you will need to convert it. And also if you want to include entered number use <= instead of <
while counttonum <= int(countnum):

Python Turtle - How do I stop the turtle at a specific distance or coordinate? [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 7 years ago.
Improve this question
This is my attempt to make the turtle stop after traveling nearly 400 pixels.
def race():
while True:
alex.forward(r_alex)
a = a + r_alex
if a > 399.9:
break
And this is what I got back
UnboundLocalError: local variable 'a' referenced before assignment
The line a = a + r_alex uses a before you actually define a.
I'm guessing a is the turtle's displacement so perhaps you should try the following:
def race():
a = 0
while True:
alex.forward(r_alex)
a += r_alex
if a > 399.9:
break
Even better:
def race():
a = 0
while(a > 399.9):
alex.forward(r_alex)
a += r_alex

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

How to show the cost of a game using this 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
How could I show the cost of a game using Python, using code something like this:
if game_list[game]>= 10:
print ("The Price for this Game is: $"), game_prices[game]*1.1
If this is python3.x you have to do it as follows:
if game_list[game]>= 10:
print ("The Price for this Game is: $", game_prices[game]*1.1)
For python2.7, it would be:
if game_list[game]>= 10:
print "The Price for this Game is: $"+game_prices[game]*1.1
You might be better to delegate the price-setting logic:
def game_price(game):
price = game_list[game]
if price >= 10:
markup = 0.1
else:
markup = 0.2
return price * (1. + markup)
print("The game's price is ${:0.2f}".format(game_price(game)))

Categories