This question already has answers here:
Generate 'n' unique random numbers within a range [duplicate]
(4 answers)
Closed 2 years ago.
So I am pretty new to Python and to coding in general and I am trying to make a random number generator which makes a list of what numbers havent been displayed yet and doesnt repeat any of the numbers that have been displayed.
Thanks if someone wants to help me with this!
code:
random.randrange(1, 50, 1) for i in range(7)
print ("number list is : " + str(res))
import random
lst = []
while len(lst) < 49:
res = random.randrange(1, 50, 1)
if res not in lst:
print ("number list is : " + str(res))
lst.append(res)
Here, you create a list lst and set your code in a while loop which focus on the length of lst. As the max of different number you can get is 49, you will stop getting random number once this condition is reached.
If your number res is not in lst, you print and append res to lst. If it is in lst, you just don't mention anything and loop again to the next "new" number.
Related
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 1 year ago.
I want to print 10 random number and I try this:
import random
number = 10
while number <= 10:
print(random.randint(0,9))
number += 1
but this print next number in the new line. I try below code to fix that but:
import random
number = 10
my_list = []
while number <= 10:
my_list.append(random.randint(0,9))
number += 1
print my_list
it display number with , and []
To print without a new line you should edit the end argument which is set to newline by default, in. You could edit the print command in your first attempt to:
print(random.randint(0,9), end = " ")
import random
randomlist = []
for i in range(0,5):
n = random.randint(1,30)
randomlist.append(n)
print(randomlist)
you can read more about it [here][1].
for the basics of printing:https://www.geeksforgeeks.org/print-without-newline-python/
This question already has answers here:
Sum of digits in a string
(10 answers)
Closed 2 years ago.
I have a string of numbers, for example 12458960. How can I go about adding them so that it would be 1+2+4+5+8+9+6+0 = 35
I have the numbers already recorded into a variable called ccn. I just need to get the sum now. I tried adding the string numbers using,
ccn[0]+ccn[1]+ccn[2]+ccn[3]+ccn[4]+ccn[5]+ccn[6]+ccn[7]
But that didnt work.
Any help would be much appreciated.
You can do this:-
a = '12458960'
res = sum(map(int, a))
print(res)
Output:-
35
Try with this one liner:
a = '12458960'
print (sum(int(i) for i in a))
Here is a very basic way:
num = '123458960'
number = 0
for n in num:
number += int(n)
print(number)
This question already has answers here:
Is there a "not equal" operator in Python?
(10 answers)
Closed 3 years ago.
So theres a small chunk of code im working on for a school project but im not sure if this while command is possile:
array = []
list_amount = int(input("Enter how many numbers will be in the list"))
while len(array) == list_amount:
array.append(int(input("Enter a number")))
In the line, while len(array) == list_amount:
I want it to be not equal to
So that it will keep letting you add numbers until the length of the array and the amount you entered are the same then the loop will break.
Replace == with !=. != is a python syntax for "not equal to".
Use the != operator:
array = []
list_amount = int(input("Enter how many numbers will be in the list"))
while len(array) != list_amount:
array.append(int(input("Enter a number")))
Just do != instead of ==. It means not equal to and is basically the opposite of ==.
array = []
list_amount = int(input("Enter how many numbers will be in the list"))
for i in range(list_amount):
array.append(int(input("Enter a number")))
This question already has answers here:
Generate 'n' unique random numbers within a range [duplicate]
(4 answers)
Closed 7 years ago.
So I have a program that randomly selects a certain amount of items from a list, but if a value is randomly selected I would like it to only print once, and make the program unable to print the same value twice. How do I do this?
import random
list = [25, 50, 75, 100]
big = int(raw_input("Big numbers: "))
y = big
b = 0
while b < y:
j = random.choice(list)
print j
b += 1
random.choice isn't the right method to use here. Use random.sample instead:
import random
mylist = [25, 50, 75, 100]
big = int(raw_input("Big numbers: "))
for num in random.sample(mylist, big):
print num
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 8 years ago.
generate 10 random integers 1-100 store them in a list. Use a loop. Use a second loop to process the list. In this latter loop, display all numbers in the list and determine the sum of the odd numbers and the sum of the even numbers. Display these sums after the second loop has ended. what's wrong?
import random
randomList = [] # create list
sumEven= sumOdd = 0
for x in range(10):
r = random.randint(1,100)
print(r),
randomList.append(r)
for x in range(len(randomList)):
if (randomList[x]%2 == 0): #even number
sumEven += randomList[x]
else:
sumOdd += randomList[x]
print "\nSum of even numbers =",sumEven
print "Sum of odd numbers =",sumOdd
In the future, please post the full error message.
That being said, print is a function. You should use parentheses with it.
https://docs.python.org/3.0/whatsnew/3.0.html#print-is-a-function