why am i getting invalid syntax? [duplicate] - python

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

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.

adding numbers in a string in python [duplicate]

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)

How to i make this while loop " not equal to" [duplicate]

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")))

Python IDLE less or more? [duplicate]

This question already has answers here:
Paging output from print statement
(3 answers)
Closed 5 years ago.
Getting a list of __builtins__ in IDLE how do I pause it after a certain number or bottom of the screen?
This gives me one at a time..
>>> for i in dir(__builtins__):
... print i
... raw_input("Press Enter...")
and I could slice it like ...
x=dir(__builtins__)
len(x)
for i in x[:10]:
print i
... and that give me first 10 but is there a way to get it to print 10, or bottom of screen until the user input ? Like a less or more in Unix?
Thanks!
Try something like:
print_every = 5
for i, f in enumerate(dir(__builtins__)):
print f
if i % print_every == 0 and i != 0:
raw_input("Press Enter...")
enumerate pairs each entry in the list with its index in the list
if i % print_every == 0: checks to see if i (the current index) is a multiple of print_every.
The above code should print the list in groups of print_every many entries.

What's wrong with my code? name is not defined? [duplicate]

This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Closed 6 years ago.
Here I've defined a function that creates a list using the argument "number in list". I can use the function but when I try to print the list it says the variable isn't defined. Anytime I try to take the declared variables out of the defined function, it tells me local variable "i" referenced before assignment. Can someone help me get this code to work? Thanks!
def create_list(number_in_list):
i = 0
numbers = []
while i < number_in_list:
numbers.append(i)
i += 1
print "How many numbers do you want in your list?"
value = int(raw_input("> "))
create_list(value)
print "The numbers: "
print numbers
for num in numbers:
print num
Your numbers variable exists only in the function create_list. You will need to return that variable, and use the return value in your calling code:
Thus:
def create_list(number_in_list):
i = 0
numbers = []
while i < number_in_list:
numbers.append(i)
i += 1
return numbers # <----
And in your main code:
numbers = create_list(value)

Categories

Resources