Python passing arguments from console [duplicate] - python

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

Related

How do I create an if statement to check if my variable is an int? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I want to make a program where I need to check the input of the user
so, the question is How do I check the input value, if it`s a string or an integer?
Here's some experimental code that didn't work:
a = 10
print(type(a))
if (a==10):
print("aaaaa")
else:
if(type(a)== "<class 'int'>"):
print("12345")
You can do it in the following way:
a = 10
if type(a) is int:
print("Yes")
else:
print("No")
input() will always return string, i.e type(input()) will be str
from my understanding, you want to validate input before proceeding further
, this is what you can do
foo = input()
if not foo.isdigit():
raise ValueError("Enter a valid integer")
foo = int(foo)
this will not work for a negative number, for that you can check if foo startswith "-"
if foo.lstrip("-").isdigit():
multiply_by = -1 if foo.startswith("-") else 1
foo = multiply_by * int(foo.lstrip("-"))
and if you want to check type of variable
a = 10
if isinstace(a, int):
pass

Python comparison if statement not working [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
I am just messing around with Python trying to do a simple guessing program. The code inside my if statement never runs, and I can't figure out why. I've tried printing both variables at the end, and even when they're the same, the comparison never resolves to true. I've also tried just setting y as 2 and guessing 2 as the input, and it still doesn't work.
import random
x = input("Guess a number 1 or 2: ")
y = random.randint(1,2)
if x==y:
print("yes")
The problem here is that x is a string and y is an int:
x = input("Try a number ") # I chose 4 here
x
'4'
x == 4
False
int(x) == 4
True
input will always return a string, which you can convert to int using the int() literal function to convert that string into the required value

while loop factorial non use break [duplicate]

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:
...

Compute the factorial of n using range with two parameters [duplicate]

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 ^.^

Python 'while'/'or' Issue [duplicate]

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.

Categories