#TODO: QUESTION 1
num = random.randint(1, 100)
print(num)
# Male or Female probabilities = 35%
if num > 0 and num <= 18:
femaleCheck(People)
printPeopleList(People)
if num > 18 and num <= 35:
maleCheck(People)
printPeopleList(People)
# Wearing a T-Shirt probabilities = 15%
if num > 35 and num <= 50:
tShirtCheck(People)
printPeopleList(People)
#Eye Color probabilities 25%
if num > 50 and num <= 63:
eyeColorBrownCheck(People)
printPeopleList(People)
if num > 63 and num <= 75:
eyeColorBlueCheck(People)
printPeopleList(People)
#Showing Teeth probabilities 25%
if num > 75 and num <= 100:
showingTeethCheck(People)
printPeopleList(People)
Here is a small portion of the code for a guess who game that I creating. As it is currently implemented it chooses a random number from a list and asks the corresponding number to carry out a function. Once question 1 is asked I would like it to ask Question 2 out of the remaining available options. Question 3 will include the remaining 2 options, and also introduce a new function to ask. I am struggling to figure out which method would be most efficient; I know that I could just indent each question following, but that would be terribly messy and hard to read. Is there some method I could use to make the following questions asked in a simplistic manner? Potentially removing that range from random integer?
If you're using 3.6 you can use random.choices with the weights keyword argument against your functions.
checkFunction = random.choices([femaleCheck, maleCheck, tShirtCheck, ...], weights=[35, 15, 25, ...])
checkFunction(People)
printPeopleList(People)
If not, check out this answer on how to implement something similar.
Related
This question already has answers here:
How do I sort a dictionary by value?
(34 answers)
Closed 5 years ago.
How to create a itterative number 10digit long by taking a base number
A Simple approach for Solution:
a=input()
while len(a)<10:
b=int(a[len(a)-1])+int(a[len(a)-2])
a=a+str(b%10)
print (a)
May This one helpful for you.
n= 16
while(len(str(n)) < 10):
print(n)
a = n %10 + (int(n/10))%10 # sum of last two digits
n *= 10
n += a%10 #adding the last
print(n)
Based on your example, I'm assuming you want to stop once you reach a 0.
Certain numbers, like 18 wouldn't reach a 0 ever, so also want to add a meximum length that the ID can be.
The following code runs the math until you reach a 0, or until it reaches 40 digits.
def getID(number)
maxlength = 40 - len(str(number))
while maxlength:
string = str(number)
answer = (int(string[-2]) + int(string[-1])) % 10
if answer == 0:
break
number = number * 10 + answer
maxlength -= 1
return number
getID(14) == 1459437
getID(15) == 15617853819
getID(16) == 1673
getID(18) == 17853819
getID(18) == 189763921347189763921347189763921347189763
18 is a number that repeats forever, so instead the loop ends once it reaches a set length.
This question already has answers here:
Least common multiple for 3 or more numbers
(32 answers)
Closed 5 years ago.
This is a Project Euler challenge where I'm trying to find the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
The logic while I came up with seems to run really slowly. It's been running for the last 4 mins and still hasn't found the number. I'm trying to figure out a) Is this logic correct? b) Why does this take so long? and c) Could someone give me a hint on an alternate logic that is more efficient.
# 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
# What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
smallest_num = 2520
while smallest_num >= 2520:
divisor = 2
while smallest_num % divisor == 0 and divisor < 21:
print("Smalles num = {} and Divisor = {}").format(smallest_num, divisor)
divisor += 1
smallest_num += 1
print("Smallest number is: {}").format(smallest_num)
This is still processing and so far my terminal looks like this
Here's your method run "properly" (using the term liberally), but as #James mentioned it will take an egregious amount of time as a loop.
divisors = np.arange(1, 21)
num = 2520
while True:
if np.all(num % divisors == 0):
print(num)
break
num += 1
A much better method (for Python 3.x). Directly from a similar question:
import functools
import math
functools.reduce(lambda x,y: x*y//math.gcd(x, y), range(1, 21))
Out[27]: 232792560
The following code works fine.
#!/usr/bin/env python
import math
#Generating primes
divisorMax = 20;
top = divisorMax + 1 #divisor max is the upper limit
p = [x for x in range(2,top)]
for num in p:
for idx in range(2,(top//num)+1):
if num*idx in p:
p.remove(num*idx)
#Solving the problem
result = 1;
for i in range(0, len(p)):
a = math.floor(math.log(divisorMax) / math.log(p[i]));
result = result * (p[i]**a);
print(result)
You are using brute force technique to calculate the number, which is easy to understand and write, but takes very much time.
I am using the Prime Factorisation technique explained here.
i am not 100% sure, if my solution is really correct, but i guess it is and it is pretty fast.
First of all, we don't need to care for all divisors, as most are multiples of each other. So best way is to count the divisors backwards, for example starting with 20 down to 1.
I had a look at the prime numbers, the solution needs to be a multiple of all primes above 10, furthermore we need to check the 20 divisor, the rest can be ignored, as when testing divisor 18, the 9 will work as well, and so on.
So i mulitplied 11 * 13 * 17 * 19 * 20. The resulting is 923780 and is divisible by at least the primes + 20.
So i would start at 923780 and test only every 923780th number.
smallest_num = 923780
steps = 923780
while True:
divisor = 19
while smallest_num % divisor == 0 and divisor > 10:
print("Smalles num = {} and Divisor = {}").format(smallest_num, divisor)
divisor -= 1
if divisor == 10:
print("Smallest number is: {}").format(smallest_num)
break
smallest_num += steps
Maybe i have logical error?!
When passing a parameter into my function, it will not recognize the list and output the string.
The game is called pass the pigs, and it is required to output a state that the pig lands on.
I know in places that the code is inefficient, though this is due to the fact that I have been trying different methods that have no succeeded :(
Thanks in advance for any help!
Here is code:
norolls = int(input("Enter the number of rolls: "))
counter = 0
def roll(nothrows,counter):
rollList = []
while counter < nothrows:
rollrand = randint(0,100)
rollList.append(rollrand)
counter = (counter + 1)
return rollList
rollList = roll(norolls,counter)
rollList = list(map(int, rollList))
listlen = len(rollList)
def rollout(List, listpos, ListLen):
listpos = 0
for x in range(ListLen):
if List[listpos] == 1< 35:
print("Pink")
elif List[listpos] == 35 < 65:
print("Dot")
elif List[listpos] == 65< 85:
print("Razorback")
elif List[listpos] == 85 < 95:
print("Trotter")
elif List[listpos] == 95 < 99:
print("Snouter")
else:
List[listpos] == 99 < 100
print("Leaning Jewler")
listpos = (listpos + 1)
rollout(rollList, counter, listlen)
I'm assuming you want if List[listpos] == 1< 35 to mean List[listpos] is between 1 and 35, with 35 not being included.
The way to write that is:
if 1 <= List[listpos] < 35:
But, in your case, you don't really need a 3 level condition, since the only the first true if statement will run. So, you can just simply do:
if List[listpos] < 35:
print("Pink")
elif List[listpos] < 65:
...
and so on.
I have too low reputation to comment, but I'm going to try and clarify the code a little and give my answer.
For starters, one thing you should know is that list is a reserved name, so I don't recommend passing it as an argument to any function. You should be passing rollList to rollout(), since that is the list that you are creating. The way to pass a list as an argument is like this:
list_name = [1,2,3,4,5]
def function_name(myList=[]):
for x in myList:
print x
function_name(list_name)
Note the myList=[] in the function definition.
I would also get rid of counter and listlen as arguments, since you are setting counter to 0 at the beginning of the function, and listlen can be found with the len() function.
Secondly, for your equality statements, type them in like this:
if list_name[listpos] >= 1 and list_name[listpos] < 35
I'm sure there's a shorter way to do it, but this would help you visualize it as a range of values.
Since there are only 100 possible rolls (you do not assign an interpretation to 0) there is an alternative approach: replace the if-elif-else change with a lookup table mapping rolls to names. The code below does this. It also creates a list of rolls with a list comprehension.
from random import randint
rollmap = [None]
for sublist in (35*['Pink'], 30*['Dot'], 20*['Razorback'],
10*['Trotter'], 4*['Snouter'], 1*['Leaning Jewler']):
rollmap.extend(sublist)
n = int(input("Enter the number of rolls: "))
rolls = [randint(1, len(rollmap-1)) for i in range(n)]
for roll in rolls:
print(rollmap[roll])
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
So basically I want to do this generate a random number (this I know how to do) if that number is even (or number%2 == 0) then divide it by 2 then if the resulting number is odd (or number%2 > 0) then multiply by 3 and add 1. If that didn't make much sense here is an exmaple
Pick a number like 26 (this is even so divide by 2)
Resulting number is 13 (this is odd so multiply by 3 add 1)
Resulting number is 40 (this is even so divide by 2)
Continue this process until the number is == 1
I am not sure what loop to use to do this so any help is very appreciated! :)
number = # generate random number
while number != 1:
if number % 2: # if number is odd, multiply by 3, add 1
number *= 3
number += 1
else: # if number is even, divide by 2
number /= 2
You can run a bit of cheeky code to keep track of iterations, if you like:
num_iterations = 0
number = # generate random number
while number != 1:
num_iterations += 1
if number % 2:
number = number * 3 + 1
else:
number /= 2
Since you do not know how many steps would it take to get the number equal to one, i.e. the number of iterations is unknown , use a while loop:
number = # random number
while number != 1:
if number % 2:
number *= 3
number += 1
else:
number /= 2
Or another approach:
number = # random number
while True:
if number == 1:
break
elif number % 2: # odd
number *= 3
number += 1
else: # even
number /= 2
Is there a way to set up an if statement like this where 1 if statement covers multiple integers?
variable = random.randrange(1,10)
if variable is between 1 - 3
then do this
if variable is between 4-5
then do this
if variable is between 6-9
then do this
or maybe something like this
a = 1,2,3,4,5,6,7,8,9,10
variable = random.randrange(1,10)
if variable == a:
then do this
What about choosing a random number for each player, then the player with the largest value gets to attack first.
Or, if this isn't turn based, then set a threshold, and if the random number is over the threshold, then the player can attack.
The threshold could also be a random number. For example:
player1.attack = randn ()
[player2 etc]
minval = randn ()
for player in players:
if player.attack > minval:
[...attack...]
The question at the bottom is easy to implement, basically exactly as you've written it:
variable = random.randrange(1,10)
if 0 <= variable < 3:
then do this
if 3 <= variable < 5:
then do this
if 5 <= variable < 9:
then do this