Make print come after `if` in python [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I'm making a quiz and when I make an if statement in python to put a question I have to put the print before it(which I'm using at answers). So my question is how to make it come after without stopping the if statement. Any ideas appreciated. Sorry if its hard to know what I'm trying to say I'm a little new to python.

Something like this maybe?
answer = input ('Who was the last king of Rome? ') #use raw_input for python2.x
if answer == 'Tarquinius': print ('correct')
else: print ('incorrect')

print 'asdf' if 1 == 2 else 'fdsa'
Or if you don't get that this does, try this:
print "ok..nice" if raw_input("how are you ") == "ok" else "well, that's too bad"
Apparently people don't know the new inline if syntax, and think i'm trolling.
This should all be written in 1 line and does not require splitting.

Related

For Loop in Python isn't working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
inputs = []
iterations = int(input())
for x in inputs:
currentInput = input()
inputs.append(currentInput)
print(x)
That code isn't working. It is supposed to make more "currentInput" variables based on "iterations". Thank you soooo, much, as this has been bugging me.
Your code isn't working because it's not right.
Your for loop is going through each element in the list inputs. But inputs is an empty list; you haven't added anything to it so the for loop won't work. You must have meant
for x in range(iterations):
currentInput=input()
inputs.append(currentInput)
print(currentInput)

How do I create and if condition for strings 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 8 years ago.
Improve this question
Hello I've been trying to get this line of code working but it keeps going down to the else: statement.
Any ideas?
I cant understand what I'm doing wrong for it not to accept the if: condition.
print('What do you get when you cross a snowman with a vampire?')
answer1 = input()
if answer1.lower 'frostbite':
print ('Huh? how did you know that YOU SPY!')
else:
print('FROSBITE!')
It's hard to tell with the code fragment since it has a SyntaxError, but I'm guessing you have:
if answer1.lower == 'frostbite':
...
The problem here is that answer.lower is a bound method which is clearly not equal to any string. You need to call it to actually generate the lower case string:
if answer1.lower() == 'frostbite':
...
Someone told me the answer
Thanks friends
print('What do you get when you cross a snowman with a vampire?')
answer1 = input()
if answer1.lower() == 'frostbite':
print ('Huh? how did you know that YOU SPY!')
else:
print('FROSBITE!')
You have to compare the strings:
print('What do you get when you cross a snowman with a vampire?')
answer1 = input()
correct = 'frostbite'
if answer1.lower == correct :
print ('Huh? how did you know that YOU SPY!')
else:
print('FROSBITE!')

"while True" intermediate solutions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm working with big multidimensional arrays. I want to know after x loops the values S,S2,E,E2
I want to find all the solutions of a loop.
I have a code that iterates with a greedy. My output is a list of numers. I want to save the arrays that generate those numbers. My code:
while True:
if f2<f:
f,S,S2=f2,E,E2 # f is a value and S and E are arrays
......
print f2-f
else:
break
If i run this code I have a list of f2-f but I need also to save the f,S,S2,f2,E,E2 created in each passage of the while.. Thank you
results = []
while True:
if f2<f:
f,S,S2=f2,E,E2 # f is a value and S and E are arrays
......
results.append((f,S,S2,E,E2))
print f2-f
print "intermediate result:", results[-1]
else:
break

Please help me understand this - Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Can someone please explain how this works? Like, how does it count?
for a in range(2,5):
for b in range(1,2):
print (a+b,end=" ")
print("---",end=" ")
the output is: 3 --- 4 --- 5 ---
It just adds 2, 3, 4 with 1 and prints the result.
In Python-3.x, print() is a function and the argument end in it means
string appended after the last value, default a newline
Values returned by range(x, y) are [x, y-1]
If you don't know the usage of a function, you could open ipython and type something like the following:
help(print)
help(range)

I have variable which asks for users input, how can i print that variable also using if/else in python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
This is my code:-
print "Welcome to the English to Pig Latin translator!"
original = raw_input("Write a word")
if len(original)>0:
print origninal
else:
print "empty"
so here original is my variable. i get the string "empty" if user inputs no words, but if user inputs any words which is more than 0 characters (as defined in if statement) i am getting an error saying "name original is not defined".
I want the console to print the users input. What is wrong in my code?
There is a typo in your code. origninal should be original
if len(original) > 0:
print original
else:
print "empty"

Categories

Resources