Divisors of a number in Python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
I've tried to write a peice of code in python that prints all the divisors of a number including itself. it doesn't work correctly can anyone see why?
def divisors(num):
for x in range (1, num):
if (num % x) == 0:
print(x)
print("Divisors of 6 are")
print(divisors(6))

range() excludes the upper bound and therefore should read range(1, num+1).

you can also try this
num=int(input("please eenter a number "))
a=[]
for x in range(1,num+1):
if num%x==0:
a.append(x)
print(a)

Related

why possibility_1=0 and possibility_2=0 doesnt changing? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
import math
import random
possibility_1=0
possibility_2=0
while True:
list = [0, 1]
number_1=random.choice(list)
number_2=random.choice(list)
number_3=random.choice(list)
number_total=str(number_1)+","+str(number_2)+","+str(number_3)
if number_total==(1,1,0) or number_total==(0,0,1):
possibility_1 +=1
if number_total==(1,1,1) or number_total==(0,0,0):
possibility_2 +=1
print(str(number_total)+" possibility_11= "+ str(possibility_1)+" possibility_12= "+ str(possibility_2))
guys the possibility_1 and possibility_2 doesn!t changing. Please help me I want to make a heads or tails code and simulate it then check the possibilities.
Looks like number_total is a string so the output would be "1,1,0" you are comparing with a tuple. This means the if statements will not be invoked.
if number_total=="1,1,0" or number_total=="0,0,1":

Invalid syntax in a variable in Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
The code is below
i=0
a=0
Matrix[0]=0
Matrix = [1 for i in range(1000)]
while i<1000:
i=i+1
Matrix[i]=(1/1000)*((i*(1/1000))^2
a=a+Matrix[i]
print (a)
When I try to run the program, Python highlight the first a in the 8th row and it says invalid syntax.
I can't figure out what is the problem with the code.
Much appreciated if you helped
Corrected Code:
i=0
a=0
Matrix=[1 for i in range(1000)]
while i<1000:
Matrix[i]=(1/1000)*((i*(1/1000)))**2
a=a+Matrix[i]
i=i+1
print (a)

Python if else executing in a bad way [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am new to python and trying to execute my code. Below is my code.
def evod(number):
if number % 2 == 0:
print("The number is even")
return"The number is odd"
print(evod(60))
Output is:
The number is even
The number is odd
if I run this function it prints both lines. It should print a single one of those. Right? Why is this happening?
if c=="July":
print("7/1/2017")
else:
print("sorry")
Indentation matters in python!
if c == "July":
print ("7/1/2017")
else:
print ("sorry")
Correct your indentation

Python in proccess of geting to pig game [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Issue occurred right after I added the and x= "J1234": I don't know what's going on.
original = ""
if len(original) > 0 and x = "J123":
raw_input("Enter a word:")
original = raw_input('word')
print "original"
else:
print "empty"
x.isalpha()
So, you're getting a SyntaxError. To fix this, change:
x = "J123":
to...
x == "J123":
In Python the former is for setting values and the latter is for checking them. Therefore when you use this the wrong way round the syntax is incorrect.

INVALID SYNTAX ERROR for 'else' statement in python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to write a quicksort program in python, however I'm getting an invalid syntax error at else statement in the second last line below:
import random
n=int(raw_input("Enter the size of the list: ")) # size of the list
intlist = [0]*n
for num in range(n):
intlist[num]=random.randint(0,10*n)
pivot=random.choice(intlist)
list_1=[] # list of elements smaller than pivot
list_2=[] # list of elements greater than pivot
for num in range(n):
if num<=pivot:
list_1.append(num)
else
list_2.append(num)
This is not a complete program as I am still writing.
add a colon after the else so that it looks like else:. and pick up a good tutorial ;)
Looks like you need a ':' after "else".

Categories

Resources