Get the sum of a function in python [closed] - python

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
My code:
# -*- coding: utf-8 -*-
def dice():
import random
number = random.randint(1,6)
print "The dice shows:" + str(number)
[dice() for _ in range(3)]
Example result:
The dice shows:2
The dice shows:4
The dice shows:3
If I want to sum all the numbers in the list, how do I do that? (In this case, I would get the sum 9)

Well, I think you should read a little bit more Python doc, because you have some doubts even before logic.
Here is what I think you wan to do.
import random
def dice():
return random.randint(1,6)
sum([dice() for i in range(3)])
Your function is printing result as string, not returning the result.

For manual suming:
import random
def dice():
return random.randint(1,6)
list = []
for i in range(3):
list.append(dice())
sum = 0
for i in list:
sum +=i
print sum
Built in suming:
print sum([dic() for _ in range(3))

Related

List Comprehension starting at a specific number [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 4 months ago.
Improve this question
I was trying to create this [80000, 104000, 135000...] list in Python. Its the value, starting at 80,000 multiplied by 1.3 each time I want
What i've tried:
a = [num*1.5 for num in ??? if num>=80000] #???--> i've tried range(10)
I should be able to do this but I can't find any solutions rn..
I must use list-comprehensions, if possible.
Some help would be nice, thank you!
There is a very basic mathematical operation that represents multiplying by the same value many time: power.
a = [80000 * (1.3**n) for n in range(100)]
You could write your own generator then use that in conjunction with a list comprehension.
def numgen(start, factor, limit):
for _ in range(limit):
yield int(start)
start *= factor
mylist = [value for value in numgen(80_000, 1.3, 10)]
print(mylist)
Output:
[80000, 104000, 135200, 175760, 228488, 297034, 386144, 501988, 652584, 848359]
import numpy as np
print(80000 * 1.3**np.arange(3))
# [ 80000. 104000. 135200.]

change the order of numbers from the back [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 7 months ago.
Improve this question
they need to order with a problem
so I have the text in a certain order of numbers, something like gematria
input [12345] is what we call gematria and what do they need?
they need to line up the digits backwards
[54321]
have a different count and I would need help with that rather than doing twenty different if
def shiftall(s, n):
n %= len(s)
return s[n:] + s[:n]
it didn't help me much it only moves the simple text
For strings:
return s[::-1]
For integers:
return str(s)[::-1]
Note: This would go inside def shiftall(s, n):
Additional note: Now you don't even need the parameter n
If you want to reverse a number, then you can convert it to a string, reverse the string, and then convert it back to a number.
num = 12345
str_num = str(num)
# reverse and convert
num = int(str_num[::-1])
input=[12345,43545436,88888,843546]
def shiftall(s):
d=[]
for i in s:
res=''.join(reversed(str(i)))
d.append(int(res))
return d
print(shiftall(input))

Printing and array two times 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 4 years ago.
Improve this question
I have a list a=['abc','cdv','fasdf'] and also have a constant n which says the amount of time print each elements two times.
For example, n=2 should return a=['abc','abc','cdv','cdv']; or n=4 will return a=['abc','abc','cdv','cdv','fasdf','fasdf','abc','abc'].
Here is one way using itertools.chain and a generator comprehension:
from itertools import chain
a = ['abc','cdv','fasdf']
n = 4
res = list(chain.from_iterable([a[i % len(a)]]*2 for i in range(n)))
# ['abc', 'abc', 'cdv', 'cdv', 'fasdf', 'fasdf', 'abc', 'abc']
it looks like you'll need to recycle elements if n is larger than the length of the list. An easy way to deal with this is to duplicated the array as many times as needed.
import math
n_over = math.ceil(len(a)/n)
n_reps = 1 + n_over
a_long = a * n_reps
we can iterate over the new array to build the new one
a_rep = []
for e in a_long[0:n]:
a_new += [e]*n

A Program That Shuffles A Deck of Cards? [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 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]))

shifting numbers in python code [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 7 years ago.
Improve this question
Please help I have no idea on how to write this function. I tried a ceaser cypher function and it didn't work. Any ideas?
Write a function cycle( S, n ) that takes in a string S of '0's and '1's and an integer n and returns the a string in which S has shifted its last character to the initial position n times. For example, cycle('1110110000', 2) would return '0011101100'.
The function you are looking for is:
def cycle(s, n):
return s[-n:] + s[:-n]
You could use Python's deque data type as follows:
import collections
def cycle(s, n):
d = collections.deque(s)
d.rotate(n)
return "".join(d)
print cycle('1110110000', 2)
This would display:
0011101100

Categories