Roll Two Dice Python 3.5.1 [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Instructions: Simulate rolling 2 die with 6 sides each 100 times and count these 3
cases
-The dice sum equals 7
-The 2 die are doubles (same number)
-The dice sum is 10,11, or 12 (greather than or equals to 10)
What I have:
from random import randint
def rolldie():
return randint(1, 7) + randint(1, 7)
n=10
for j in range(n):
print(str(j) + ". Outcome: " + str(rolldie()))`
Overall I don't know if this is correct. Looking for more help. Thank you.

You need to return the values of both dice, not their sum, so you can compare whether they were each the same value.
def roll_dice():
return (random.randint(1,6), random.randint(1,6))
equal_7 = 0
doubles = 0
ten_or_more = 0
for i in range(100):
d1, d2 = roll_dice()
if d1 + d2 == 7:
equal_7 += 1
if d1 == d2:
doubles += 1
if d1 + d2 >= 10:
ten_or_more += 1

Related

Indexerror:::: list index out of range [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
hi this is my code and I don't know why I received this type of error
x = int(input())
n = [int(i) for i in input().split()]
middle = n[int((x - 1) / 2)
even = 0
odd = 0
for number in n:
if number % 2 == 0:
even += number
else:
odd += number
answer = even * odd + middle ** 2
print("{} x {} + {}^2 = {}".format(even, odd, middle, answer))
It produces an error as such IndexError: list index out of range because n has the minimum number of list values entered by the user.
Since your intention was for n to have more values than the minimum number of digits entered by the user, I added .range() to the list iteration.
Here is the modified code:
x = int(input("Put in a number: "))
n = [int(i) for i in range(int(input("Put in another number: ")))]
middle = n[int((x - 1) / 2)]
even = 0
odd = 0
for number in n:
if number % 2 == 0:
even += number
else:
odd += number
answer = even * odd + middle ** 2
print("{} x {} + {}^2 = {}".format(even, odd, middle, answer))
If you have any questions or need clarification, please do not hesitate to ask.

How to increment fraction/denominator in loop? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm trying to develop a Python 3 program that will allow the user to choose a denominator and evaluate an equation. For Example:
The user selects 5 to be the denominator
The equation will increment until the given denominator
(1/1) + (1/2) + (1/3) + (1/4) + (1/5)
The output should be (2.283333)
My code:
d = input ("Select a denominator")
for i in range(d)
d += (1/d)
print(d)
So far I'm only able to ask the user for the input/denominator. I tried putting it in a loop but I am doing something wrong. This is what prints out:
5.2
5.392307
5.577757
5.757040
5.930740
final_denominator = int(input("Select a denominator: "))
total = 0
for i in range(1, final_denominator + 1):
total += 1 / i
print(total)
You need to convert the inputted string to an integer with int()
Use a different variable for the total and the final denominator.
range(x) goes from 0 to x - 1, to go from 1 to x you need range(1, x + 1).
You need to add 1 / i rather than 1 / final_denominator to the total.
Alternatively, this is a good use for a generator expression:
final_denominator = int(input("Select a denominator: "))
total = sum(1 / i for i in range(1, final_denominator + 1))
print(total)

list index out of range with if condition [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
num_array = list()
num = int(raw_input("Enter how many elements you want:"))
print 'Enter numbers in array: '
for i in range(int(num)):
n = raw_input("num :")
num_array.append(int(n))
print 'ARRAY: ',num_array
b = 0
count = 0
while b< num -1 or b>0:
count = count+1
if num_array[b]!= 0:
b = b + num_array[b]
else:
b = (b + num_array[b])*2
print count
i am trying to get an array as an input anD storing a index value in a variable which is updating its values. But i am getting an error of list out of range
Please help me
Your condition while b < num-1 or b>0 does not make sense. The or b>0 part makes it True for all values larger than num-1. Instead, you should use and:
while b < num-1 and b > 0:
Or shorter, using comparison chaining:
while 0 < b < num-1:
Also, note that array indices go from 0 to num-1 (num being the length), so actually the condition should probably be (not tested, though):
while 0 <= b < num:

Python ( Heads or Tails Counter ) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to create a simple heads or tails program. For some reason it's not working and I have no idea why. Any ideas?
import random
counter = 0
flip = random.randint(1,2) # Initializing Values of variables
heads_counter = 0
tails_counter = 0
if flip == 1:
heads_counter +=1
counter +=1
elif flip == 2:
tails_counter +=1
counter +=1
else:
print("Invalid flip")
print("Flip is",flip)
print("Flipped The coin",counter,"Times")
print("Landed Heads",heads_counter,"Times")
print("Landed Tails",tails_counter,"Times")
In order to get the numbers to increment, you must have multiple flips during one execution. Here is a much simpler implementation of what you are doing that runs the program num times.
To flip the coin 100 times, simply set num = 100
from random import randint
# number of coin flips to simulate
num = 100
# simulate the coin flips
flips = [randint(1,2) for x in range(num)]
# count up the results
heads = flips.count(1)
tails = flips.count(2)
# print the results to the console
print("{0} Coin Flips".format(num))
print("{0} Heads".format(heads))
print("{0} Tails".format(tails))
This gives you output like the following:
>>> 100 Coin Flips
>>> 49 Heads
>>> 51 Tails

Multiplication without using the times or division symbol in python [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
else:
while int(n2) - 1 != 0:
a = int(n) + int(n)
print("" + str(a))
I need this part of code to times n by n2 without using '*' or '/' and I'm not sure how to change this so that it will work. What do I need to change/add to make this work?
Something like this:
lowest, highest = a, b
if b < a: lowest, highest = b, a
total = 0
for _ in range(lowest):
total += highest
print "a x b = %s" % total
You can use a for loop to add n to ans exactly n2 times:
n = 30
n2 = 2
ans = 0
for i in range(n2):
ans += n
print(ans)
If you need to operate on strings (as in your question), you could use the following example:
n = '30'
n2 = '2'
ans = 0
for i in range(int(n2)):
ans += int(n)
print(str(ans))

Categories