I write a program in python to find no of combinations of series
there is one dice which has 6 faces.
user input 2
then the out is shown is as no of count where two is come in combinations
ex if we throw dice for to get 2 as sum the maximum are two dice thrown are required
(1+1) and (2) so count is 2
if i throw for sum of 3, the out-put is
(1+1+1),(1+2),(2+1),(3) so count is 4 enter code here
if i throw for sum of 4 then the out put is
(1+1+1+1),(1+1+2),(1+2+1),(2+1+1),(2+2),(3+1),(1+3),(4) count is 8
I write the code is
# I am considering the Board is horizontal single line
def count_values(values,num):
for i in range(num):
print(values[i]," ",end='')
print('')
def print_list(out_put,values,num,count=0,show=False):
dice=6
if num == 0:
count_values(values,count)
out_put[0] += 1
elif num > 0:
for k in range(1,dice+1):
values[count] = k
print_list(out_put,values,num-k, count+1,show)
n=int(input('Enter A number'))
values=[0]*n
out_put=[0]
print_list(out_put,values,n)
print(out_put)
it shows out put for small inputs likes 10,20,30
but
i want the out put for 100 and 500 and 610 like inputs ,
but is get more time (around 5-6 hours still running) and the count of combination is more than 1145201564
still it is counting
any one has solution for this
Any one has any solution. for this
import numpy
def findway(m,n,x):
table = numpy.zeros((2,x+1))
for j in range(1,x+1):
table[1][j] = 1
for j in range(1,x+1):
for k in range(1,j):
table[1,j] += table[1][j-k]
print table[1][x]
n=input('Enter a number')
findway(6,1,n)
but here is also one problem, i want out put for 600 ,but it shows out-put in format (2.07475778444e+180)
but i want in integer format
Related
#Add the input number 4 times. Ex. 3+3+3+3
#If the input is 3, the output will be 12
num = int(input("Num: "))
for x in range(2):
num += num
print(num)
Using an app called "Easy Coder" and
for some reason, the code above is not correct.
Is there a better way to do this? so the actual process of the code is("3+3+3+3) and not(3+3) + (3+3)
Edit: Sorry, I forgot to mention, that this is an exercise related to
looping.
The task:
Write a program that uses a for loop to calculate any
number X 4.
Hint - Add the number to a variable 4 times.
To add 4 times, use a separate variable for the total and loop 4 times instead of twice
num = int(input("Num: "))
total = 0
for _ in range(4):
total += num
print(total)
You can always create another variable to store the total, or you could simply multiply the number initially by 4 instead of using a loop. Otherwise, like you said, your code is actually doing:
3 + 3 = 6
6 + 6 = 12
since you overwrite the initially supplied number.
I wrote this from pseudo-code provided during class, and I've got most of it figured out. Only issue I'm running into is it's returning duplicate numbers for the first 5 'balls' and I can't at all figure out why. One of the lines in the pseudo-code I wasn't sure about was: "if that number is not in the main number sequence". I coded it like this:
if number != mainNumbers:
which could be the issue, but I'm not sure how else to code that.
from random import *
def drawing():
balls=0
mainNumbers=[]
while balls < 5:
number=randint(1,69)
if number != mainNumbers:
mainNumbers.append(number)
balls = balls + 1
mainNumbers.sort()
pBall=randint(1,26)
return mainNumbers, pBall
def main():
print("This program simulates a user defined number of Powerball drawings\n")
runs = int(input("What's the total number of drawings? "))
print()
count = 1
while count <= runs:
balls, pBall = drawing()
print("Drawing: {0} - The numbers are: {1} and the Powerball is: {2}".format(count, balls, pBall))
count = count + 1
main()
my 6th grade teacher asked me to code a program in python that input any amount of a number and it outputs the mean so i tried and theirs a problem with the print statement here's the code.i even tried to replace the float(b) statements to float(1) statements but that didn't work either
n = int(input("Enter number of elements :"))
b = input("\nEnter the numbers :")
sum = (float(b)+float(b)+float(b) / float(n)
print("\nThe mean is -",sum())
Please give space separated input here :
input = list(map(float ,input('Enter number of elements : ').split(' ')))
mean = sum(input)/len(input)
print(mean)
: Output :
Enter number of elements : 10 20 30
20.0
i would do something like this.
I assumed you wanted the arithmetic mean and not the geometric one, so the first thing to do is to get the number of values the user wants to input and store it in the variable n. The you iterate over a range n and you sum the inputs to k.
The you divide k by n.
n, k = int(input("Enter number of elements:")), 0
for i in range(n):
k += float(input("Enter value number {}".format(i+1)))
k /= n
print(k)
if you want to use the "cool" way you can do something like this:
n = int(input("Enter number of elements:"))
k = sum([float(input("Enter value number {}".format(i+1))) for i in range(n)]) / n
print(k)
There is an syntax error and logical error in this code.
You haven't closed the opening bracket, It should be like this
sum = (float(b)+float(b)+float(b)) / float(n)
If you want to calculate the mean of 3 values why are you adding the same value for three times here
(float(b)+float(b)+float(b))
Insted of using same values you can get each values from for loop like this
for i in range(3):
num = float(input())
total += num
Recently I was given a challenge to my coding skills by my teacher since he saw that I was already knowledgeable in what he was teaching. The question is as follows.
Create a program that prompts the user for 2 numbers. The program will then display all the prime numbers in between the two given numbers, including the given numbers. Note: You cannot assume the first input is bigger than the second input.
So I took this question and built a fairly simple algorithm and ran it and it worked. I opened it today to find out for some reason my output is occasionally wrong, for example when you input 8 and 29 I get 27. I am looking for HINTS as to what is wrong with my logic because I cannot for the life of me figure out what Im doing wrong. I dont want straight up fixes because I would like to learn as much from this and doing it as much as possible by myself.
numbers = [int(input("First Number")), int(input("Second Number"))]
numbers.sort()
numList = []
#Removing Even Numbers
for num in range(numbers[0],numbers[1] + 1):
if num % 2 != 0:
numList.append(num)
#Checking For Prime Numbers
for currNum in numList:
#Set Start number to divide
i = 2
while i < currNum:
#Checks if the currNum can be divisble by i and is a whole number
if currNum % i != 0:
i = i + 1
else :
numList.remove(currNum)
break
print(numList)
From what I have learned from testing this out it seems like 27 is never checked during my for loop or while loop even though it is in the numList array.
Never remove items form a list you are iterating over.
Instead create a new list:
numbers = [int(input("First Number")), int(input("Second Number"))]
numbers.sort()
primes = []
for num in range(numbers[0], numbers[1] + 1):
#Set Start number to divide
i = 2
while i < num:
#Checks if the currNum can be divisble by i and is a whole number
if num % i == 0:
break
i += 1
else:
primes.append(num)
print(primes)
I am a beginner programer, and I have to create a program that simulates rolling a dice 10000 times, and then tells the user how many times they rolled each number. The hard part is, I have to do it using only two variables.
import random
count1=0
count2=0
count3=0
count4=0
count5=0
count6=0
dice=random.randint(1,7)
for i in range(10000):
if dice==1:
count1+=1
if dice==2:
count2+=1
if dice==3:
count3+=1
if dice==4:
count4+=1
if dice==5:
count5+=1
if dice==6:
count6+=1
print "You entered "+ str(count1)+ " ones, " + str(count2) + " twos, "+str(count3) + " threes, " +str(count4)+ " fours, " +str(count5)+ " fives, and "+str(count6) +" sixes."
The problem is, I can't get the program to pick more than one random number, it will just repeat the same number 10000 times. Also, as you can see, I have no idea how to write this program with only two variables, but I think it may have something to do with lists.
You need to put the line dice=random.randint(1,7) inside the for loop.
Your immediate problem is that you get a random value once before the loop starts, and then use that single value each time through the loop. To fix this, the call to random.randint() should be moved inside the loop:
for i in range(10000):
dice=random.randint(1,7)
if dice==1:
Secondly, the call as you have it will give you numbers between one and seven inclusive which, unless you're using some weird Dungeons and Dragons style dice, is probably not what you want. You should be generating numbers from one to six inclusive or, as per below with the arrays, zero through five.
Thirdly, if you're limited to two variables, it's almost a guarantee that you'll use those up with a loop counter and an array of count values, leaving nothing left over for the dice throw and individual count variables.
So you'll need something like the following pseudo-code:
dim count[1..6]
for i = 1 to 6 inclusive:
count[i] = 0
for i = 1 to 10000 inclusive:
count[random(1..6)] += 1
for i = 1 to 6 inclusive:
output "Dice value ", i, " occurred ", count[i], " times."
If this is classwork, I urge you to stop reading now and implement your own solution based on that. If it's not classwork, or your ethics aren't as strong as they could be (a), the Python code below shows one way of doing it, keeping in mind that Python arrays are zero-based rather than one-based:
import random
count = [0, 0, 0, 0, 0, 0]
for i in range(10000):
count[random.randint(0,5)] += 1
for i in range(6):
print "Value %d happened %d times" % (i + 1, count[i])
(a) Just be aware that SO is a fairly well known site and your educators may well be checking classwork against things they find on the net.
you can use a dictionary:
import random
from collections import defaultdict
dice = defaultdict(int)
for x in range(10000):
dice[random.randint(1,6)] += 1
print(dice)
import random
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
for i in range(10000):
dice = random.randint(1, 7)
if dice == 1:
one += 1
if dice == 2:
two += 1
if dice == 3:
three += 1
if dice == 4:
four += 1
if dice == 5:
five += 1
if dice == 6:
six += 1
print(one, two, three, four, five, six)
Because you put dice=random.randint(1,7) outside the loop which of course will always only generate one number.
I will put code for 'only two variables' later. Looks like an interesting question.