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

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.

Related

unwanted result from a function and if else [duplicate]

This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 months ago.
I'm trying to create a simple program using function and if else. Even though there is no errors, my program doesn't give expected result
value1 = float(input("Enter your first number:"))
value2 = float(input("Enter your second number:"))
question= input("choose mode:" )
add = True
def nor_cal(num1, num2):
if question == add:
total = num1+num2
print("the total is: ")
print(total)
else:
print("Invalid operator")
result1 = nor_cal(value1, value2)
print(result1)
when i run the program, it shows like this:
Enter your first number:2
Enter your second number:3
choose mode:add
Invalid operator
None
I don't know where i'm wrong please help !
The bug is in the line if question == add:, what you're asking the program to do is to compare the variable question (which is "add") to the variable add (which is True).
You're going to want to use if question == "add": instead.

Exception if user enters a string instead of an integer [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
I am trying to make my program print(invalid input, try again) if the user types a string instead of an integer.
num_dice = 0
while True:
if num_dice < 3 or num_dice > 5 or num_dice:
num_dice = int(input("Enter the number of dice you would like to play with 3-5 : "))
print("")
print("Please enter a value between 3 and 5.")
print("")
continue
else:
break
You can simply use the isnumeric keyword to check if it is an absolute number or not.
Example:
string1="123"
string2="hd124*"
string3="helloworld"
if string1.isnumeric() is True:
#Proceed with your case.
inputs=string1
Documentation reference : https://www.w3schools.com/python/ref_string_isnumeric.asp
P.S. this will require you changing your input to string format, as isnumeric validates only string.
This below part I mean.
num_dice = str(input("Enter the number of dice you would like to play with 3-5 : "))

Python random variable not properly working [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
Helloo
I'm trying to make a simple game where you have to choose either 1 or 2 and one of them is correct. I've used a simple random generator to choose either 1 or 2 as the correct answer.
def guess():
print("")
print("Select a number, 1 or 2")
print("")
from random import randint
ran = randint(1, 2)
nmr = input("")
if nmr == ran:
print("That's correct!")
else:
print("Wrong number")
Every time I answer it prints "Wrong number".
I've also tried printing the random number before answering but it still takes it as incorrect. Any idea what is wrong there?
The problem is that you are comparing a string with an int. That always gives False.
ran = randint(1, 2) #int
nmr = input("") #str
So for it to work, either convert ran to str or nmr to int

Inputting using a function [duplicate]

This question already has answers here:
How do I pass a variable by reference?
(39 answers)
Closed 6 years ago.
I'm just starting out with Python.I'm making a basic calculator.
When I input a number ,I have to do it like this :
Number= int(input("Enter your number"))
And I was thinking if I could make a function ,like this:
def inputting (Number):
Number= int(input("Enter your number")
And then call it whenever inputting.But I have to define a variable before I use it in a function,and that can only be done by assigning a value.
And instead of the value entered , it takes the value previously assigned when I use it later.
def inputting (Number):
Number= int(input("Enter your number")
FirstNum= none
inputting(FirstNum)
print (FirstNum)
and instead of printing the value I'd typed in there, it just prints none
How do I make this work?
You need to use return:
def inputting(my_number):
return int(my_number)
Or:
def inputting():
return int(input("Enter your number"))
my_number = inputting()

Python passing arguments from console [duplicate]

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

Categories