How to print some inputs from user in python? [duplicate] - python

This question already has answers here:
Assignment Condition in Python While Loop
(5 answers)
Closed 5 years ago.
I am learning python. I tried to the following code but it did not work.
How can I print inputs from a user repeatedly in python?
while ( value = input() ):
print(value)
Thank you very much.

Assignments within loops don't fly in python. Unlike other languages like C/Java, the assignment (=) is not an operator with a return value.
You will need something along the lines of:
while True:
value = input()
print(value)

while true is an infinite loop, therefore it will always take an input and print the output. value stores the value of a user input, and print prints that value after. This will always repeat.
while True:
value = input()
print(value)

Use this code
while 1:
print(input())
If you want to stop taking inputs, use a break with or without condition.

Related

Why does this code only return 'Greetings!' regardless of input? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
I'm working through some practice questions in the book "Automate the Boring Stuff," and I'm struggling to understand why the code below only produces 'Greetings!' as output.
print('What does spam equal? Hint: Try a number between 1 and 3')
spam = input()
if spam == 1:
print('Hello')
elif spam == 2:
print('Howdy')
else:
print('Greetings!')
It does because what you have given as input is stored as string in spam . And when you are using if else statements then it is comparing with integers, but your actual value is a string, therefore it always turns out to be in the final else statement printing Greetings!
So use spam=int(input()) instead.
This is because the input() returns a string, which is never equal to an integer.
Try
spam = int(input())
instead. This will of course throw a ValueError if the input is not an integer.

How to prevent Python script from printing even though conditions to print are True? [duplicate]

This question already has answers here:
How do you get the logical xor of two variables in Python?
(28 answers)
Closed 3 years ago.
I want my Python script to print a list only if one condition is true between two separate conditions. However, it is possible for both conditions to be true at the same time. Below is the portion of code I am referring to:
pricelist = [main(), main()] #these list values are added from an earlier function in my script
i = 1
while i == 1:
pricelist.append(main())
pricelist.pop(0)
if pricelist[-1] == ("$0.00"):
pass #if the last append to pricelist is "$0.00" I don't want anything to print
if pricelist[-2] != pricelist[-1]:
print(pricelist) # Print the contents of pricelist only if the 2 values are different but NOT print anything if the last append is "$0.00"
It is possible to have the list results be [‘some value’, ‘$0.00’]. In this case, even though both conditions I have listed in my script are true, I don’t want pricelist to print anything. What do I need to change in my script so pricelist won’t print if both conditions are true?
Think about what is the actual condition for printing. If only one of them may be true and the other one not, then what can you figure? Look at the piece of code below and observe that the two conditions may not be equal to each other, simple as that!
cond1 = pricelist[-1] == ("$0.00")
cond2 = pricelist[-2] != pricelist[-1]
if(cond1 != cond2):
print(pricelist)

What is the shortest way to set a value of a variable based on user input [duplicate]

This question already has answers here:
Using a string variable as a variable name [duplicate]
(3 answers)
Closed 3 years ago.
I have some large number of boolean values e.g
bool1 = False
bool2 = False
...
booln = False
User runs the program and provides the name of a bool to be set to True. E.g python run.py bool100.
Is there a way to implement it in one or few lines without having n-depth if elif statement?
Thank you
EDIT: clarification -- these are plain bool variables and I cannot convert them into dictionaries or lists
Use exec to run python commands from a string
exec is a function that will let you pass a python command as a string. E.g.
exec('a = 1')
exec('print(a)') # prints 1
So the full solution is
# Create all the bools
for i in range(1, n+1):
exec('bool{0} = False'.format(i))
# Take user input and set the bool
idx = input("Enter the bool id") # raw_input for python2
exec('bool{0} = True'.format(idx))
This has security risks in a real app. Highly discouraged. As others have mentioned, a dict should be preferred.

Python - Using while loops in functions [duplicate]

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 5 years ago.
I'm trying to create a function that uses a while loop to count up from one to a number given by a user. The code executes as I intend it to but returns None at the end. How do I get rid of the None? Here's the code.
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
print (printFunction(int(input())))
You can use this code to prevent none, tough its just the last line changed
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
printFunction(int(input()))
In the last line you were using
print(printFunction(int(input()))) which was getting you None after printing the results.
Instead just use printFunction(int(input())). This will not print None. You can also use a message to ask user like printFunction(int(input("Enter a number"))). Since there is noting getting returned you no need to use print.

Python method not returning value [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 5 years ago.
I am having issues with values being returned exactly as they are passed to a method despite being modified inside the method.
def test(value):
value+=1
return value
value = 0
while True:
test(value)
print(value)
This stripped-down example simply returns zero every time instead of increasing integers as one might expect. Why is this, and how can I fix it?
I am not asking how the return statement works/what is does, just why my value isn't updating.
You need to assign the return'd value back
value = test(value)
Use this :-
def test(value):
value+=1
return value
value = 0
while True:
print(test(value))
You weren't using the returned value and using the test(value) inside the print statement saves you from creating another variable.

Categories