how to print random number 10 times in one line [duplicate] - python

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/

Related

random nr gen python (title isnt working constantly) [duplicate]

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.

How to continue a loop on a new line?

I'm new to python and have a problem that asks to output "#"a certain number of times based upon the value that the user entered. However it also asks to do it twice on two different lines.
I understand that I need to utilize a loop to output the "#" character.
num = int(input())
counter = 0
while counter != num:
print("#", end='')
counter = counter + 1
In the case of num = 3, the output I receive is ### however, it is supposed to be
###
###
This looks like a bit of a trick question. You are on the right path requiring a loop but you need to loop the required number of times eg
NUMBER_OF_LINES = 2
num = int(input())
# Loop the required number of lines
for _ in range(NUMBER_OF_LINES):
# Print the number of "#" symbols. Multiplying a string duplicates it.
print("#" * num)
That will produce the required results.
Is this what you want:
num = int(input())
num_of_lines = 2
for i in range(num_of_lines):
counter = 0
while counter != num:
print("#", end='')
counter = counter + 1
print()

How to count the amount of digits in an input? [duplicate]

This question already has answers here:
Correct output for function that counts occurrences of each digit in a string
(3 answers)
Closed 5 years ago.
I would like to count the amount of numbers in an input for example in the four digit number 4256 how would I find out how many fours, twos, fives and sixes there are. I would be looking for the amount of numbers in a random number.
EDIT:
I continued to work on this and this is what I got. Apologies for asking a duplicate question.
import random
import random
guess = random.randint(1000,9999)
guess = str(guess)
correct = 0
print(guess) #this was only here for testing
number = input("Enter ")
list(number)
if number[0] in guess:
correct = correct + 1
print (correct)
To count how many times x appears in y, use
z = y.count(x)

On python, how can i print up to a certain amount of random numbers from a list of generated numbers? [duplicate]

This question already has answers here:
How can I randomly select an item from a list?
(17 answers)
Closed 8 years ago.
I have it set so the user enters a max input and then the program figures out which numbers are prime and then prints it all to a file. But at the moment it only prints the closest prime number to the upper input. I need it to print 20 random numbers and put them on a file. Also with print (num) 20 times it can print 20 different outputs, but if I add print (num) 20 times it prints the same number 20 times
import random
#Always start at 2
lower = 2
#Take input from the user
upper = int(input("Enter maximum size for prime number to be: "))
for num in range(lower,upper + 1):
if num > 1:
#Prime numbers are greater than 1
for i in range(2,num):
if (num % i) == 0:
break
#End program if number is 0
#Print results
else:
randomprimes =(num)
primes =(random.choice(randomprimes))
import sys
former, sys.stdout = sys.stdout, open('Prime Number Generator Output.txt', 'w')
print (primes)
results, sys.stdout = sys.stdout, former
results.close()
random.choice from the random module returns a random item from a given iterable.
If you want 20 random items from a given list without repeating, use random.sample. (Thanks, Hugh Bothwell)
edit: Wouldn't normally do this, but here's a solution:
import random, math
upper = int(input("Enter maximum size for prime number to be: "))
primes = [x for x in range(2, upper) if x not in [j for i in range(2, math.ceil(math.sqrt(upper))) for j in range(i*2, upper, i)]]
with open("Prime Number Generator Output.txt", "w") as f:
for prime in random.sample(primes, min(len(primes),20)):
f.write(str(prime) + "\n")

why am i getting invalid syntax? [duplicate]

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

Categories

Resources