This question already has answers here:
Python "if" statement syntax error
(5 answers)
Closed 5 years ago.
I am trying to compute the factorial of n using for loop and an accumulator. I am having trouble with the range command and its two parameters - start and end. I am getting invalid syntax error. Here's the code:
# factorial.py
# Program to compute the factorial of a number
# Illustrates for loop with an accumulator
def main():
n = int(input("Please enter a whole number: "))
fact = 1
for factor in range(1, (n + 1))
fact = fact * factor
print("The factorial of", n, "is", fact)
main()
Where's the problem?
I am using Python 3.6.
You simply forgot a : after your range function since it is a for loop ^.^
Related
This question already has an answer here:
Return value of 'print' function?
(1 answer)
Closed 6 months ago.
def hourstominutes(minutes):
hours = minutes/60
return hours
h = int(input(print("Enter the number of minutes:")))
print(hourstominutes(h))
Because you are adding the function print() within your input code, which is creating a None first, followed by the user input. Here is the solution:
def hours_to_minutes(minutes):
hours = minutes/60
return hours
h = int(input("Enter the number of minutes: "))
print(hours_to_minutes(h))
Output:
Enter the number of minutes: 50
0.8333333333333334
Input is printing the result of print("Enter the number of minutes:", and print() returns None. What you want is int(input("Enter the number of minutes:")) with no print().
This question already has answers here:
Fastest way to list all primes below N
(39 answers)
Closed 3 years ago.
This program generates prime numbers . It works well but I want to speed it up as it takes quite a while for generating the all the prime numbers
#!/usr/bin/python
#intgr = int(raw_input ("Please enter your number: "))
intgr = 50000
for i in range (2, intgr+1):
j = 2
while j<i:
if (i%j) == 0:
break
j += 1
if j == i:
#print "prime", i
pass #print "prime", i
print "done"
it takes about 15 seconds to run right now i would like to decrease that time.
Generating prime numbers is inherently slow. The algorithm you implemented is known as Trial Division and is one of the slowest ways to generate primes. There are other algorithms which are much faster, for example the Sieve of Eratosthenes. I suggest you do some more research about better algorithms.
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 5 years ago.
I am a beginner who started Python a few days ago.
I'm writing a code to study and to get a Factorial. I want to write a code to terminate the program when a negative number is entered (without the break statement), but the code below has not progressed for several hours. I hope you can help me!
This code works, but the condition I want to satisfy is not to use break, but to exit the program if a negative number is entered
Code >>
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
while True:
num = int(input("Enter a number: "))
if num < 0:
continue
print(str(num) + "! =", factorial(num))
Maybe:
num = <any positive number>
while num >= 0:
...
This question already has answers here:
python: while loop not checking the condition [closed]
(3 answers)
Closed 8 years ago.
In the following code:
def foo(n):
print "n value before if:",n #displays given num
if n <= 2:
print "n value:",n #not displayed even n is less than 2
num = raw_input()
print foo(num)
The if statement does not execute on giving inputs less than 2 for num.
So, why is if statement not executing?
raw_input returns a string, you are then comparing it to an integer.
Try converting it to an int:
num = int(raw_input())
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 9 years ago.
I'm creating a program in python that involves integers and i want a piece of code to work like this:
num = int(input("Select a number: "))
while num != (1 or 2 or 3):
num = int(input("Select a number: "))
PLease can you give me the correct code for this, Thanks
You need to use in here:
while number not in (1, 2, 3):
For the part of asking for numbers check raw_input.
For the while loop, you can check while loop.