when i executed the below code it is not printing ("yes we can"), its only printing ("all the best").
I am running the code in the visual studio.
Executing statement inside if block is not printing in python.
code:
team = input("your favorite team")
if team == "Ferrari" :
print("yes we can")
print("all the best")
enter image description here
may anyone please help me.
The image you show indicates a leading space before "Ferrari" which you don't have in your prompt. The strings must be identical. When I run your code:
your favorite teamFerrari
yes we can
all the best
But you show a leading space:
your favorite team Ferrari
all the best
You can fix this in a number of ways. First choose a better prompt. Second, strip leading and trailing whitespace:
team = input("Your favorite team: ").strip()
Related
I was trying to make a name input that whenever you type a name it places on the welcome sign of the game. The only problem I am experiencing is that the name I input never has spaces between it, despite that I placed an operator to give it a space
name = input("what is your name:")
print("Welcome to the Casino" + name + "now go play till get broke")
and this is the output result: Welcome to the CasinoMikenow go play till get broke
You could always just add a space after 'Casino' and before 'now'. A better way would be to use an fstring.
For example:
name = input("what is your name: ")
print(f'Welcome to the Casino {name} now go play till get broke')
Output:
what is your name: Jordan
Welcome to the Casino Jordan now go play till get broke
Edit: Here are a few resources on format string literals:
'f-Strings: A New and Improved Way to Format Strings in Python' article on RealPython.com
'Python f-strings: Everything you need to know!'article on DataGy.io
Their are many ways to solve this problem for example:
You can add space after the word "Casino" and before the word "now"
print("Welcome to the Casino" + name + "now go play till get broke")
You can use "," instead of using "+" to separate multiple values in print statement:
print("Welcome to the Casino", name, "now go play till get broke")
"Welcome to the casino " + name + " now go play"
type it like this add a space at the end
Or just
name = input("what is your name: ")
print(f"Welcome to the Casino {name}")
I am trying to solve this problem many times but I am not able to solve i tried everything but it shows error message everytime
a=int(input('enter the first number')) #we ask for input
b=int(input('enter the second number'))
while True:
choice=int(input('enter the number corresponding to the operation you want to \nperform \n1)addition \n2)subtraction \n3)multiplication \n4)division '))
#we ask for the user choice
if choice==1: #if the user opts for addition
addition=a+b
print(f'the addition of the 2 numbers is {addition} ') #ans
ans=input('want to try another operation? yes or no')
if ans=='yes': #if the user whishes to use another operation
continue
else: #if the user opts out
print('thank you for using')
break
elif choice==2:
subtraction=a-b #same for others but diffrent opperation
print(f'the subtraction of the 2 numbers is {subtraction} ')
ans=input('want to try another operation? yes or no')
if ans=='yes':
continue
else:
print('thank you for using')
break
elif choice==3:
multiplication=a*b
print(f'the multiplication of the 2 numbers is {multiplication} ')
ans=input('want to try another operation? yes or no')
if ans=='yes':
continue
else:
print('thank you for using')
break
else:
division=a//b
remainder=a%b
print(f'the division of the 2 numbers is {division} \nand the remainder is{remainder} ')
ans=input('want to try another operation? yes or no')
if ans=='yes':
continue
else:
print('thank you for using')
break
the error message is
File "calc_of_2_nos.py", line 15
break
^
IndentationError: unindent does not match any outer indentation level
I don't know how to solve this problem I think I have indentated it perfectly four spaces
This happens to me in Sublime Text sometimes if I mix up tabs and spaces. I am answering specifically for Sublime Text because that is what you say you are using in the comments.
In Sublime Text, when you highlight whitespace you can actually see if it is a tab or a space, like this:
Notice the two arrows on the left. Where you see dots, there are spaces, and where you see lines, there's a tab.
To make your spacing consistent, I recommend that you either go back through manually and delete and then manually and consistently re-indent your code, or use a linter to redo the indentation. Sublime Text provides some tools for this (view -> indentation -> *), and tools exist online.
Another trick in Sublime Text that can help is to search the file for tabs and replace them with (for spaces).
I could not find any error.You might want to add a newline after division in your menu choice, else its perfect!
When I run this simple script and enter information an error appears which says: Name error: name'...' is not defined.
Name = str(input("Enter the name of your friend: "))
Age = int(input("Enter age of that friend: "))
print("The name of your friend is {} and their age is {}".format(Name, Age))
To expand on Tarun Guptas answer, the convention in python is that function names always start with lowercase letters. Uppercase first letters are reserved for classes. All the inbuilt functionality of python sticks to these conventions. The same is true for variable names, never start them with uppercase letters.
If you find yourself hunting a lot after syntax errors, get yourself an editor with a good python linter, that is, inbuilt functionality that checks the code on the fly. This will help you make quick progress in avoiding the most common mistakes.
As you advance in python, you should really check out the python style guide. Most of the conventions in python are not strictly mandated by the language, however if you ever plan to interact with other programmers, they will hate you with a passion you if you don't stick to pep8.
I have modified your code to the correct state:
name = str(input("Enter the name of your friend"))
age = int(input("Enter age of that friend"))
print("The name of your friend is {} and their age is {}".format(name, age))
There were three errors:
p of print needs to be lowercase and
a closing parenthesis was missing
f of format also needs to be in lowercase.
I'm going through a Book which, at this point in the book, requires me to make a small videogame that calls functions, uses if's, while's -- essentially all the things covered in the book so far. But, I get this error in this part of my code:
Code edited, get a new error.
File "ex35_study.py", line 24
third_scenario_code()
IndentationError: unindent does not match any outer indentation level
Here is my code:
options_thirdscenario_actions = ['Examine the door', 'Try to force it']
def third_scenario_code():
print "Let me try to crack this thing up, says Lars as he starts to type in the panel. You hear the sounds of the fight out there, there's not much time left. "
print "After receiving several commands a window with a code pop ups. "
print codefile.read()
def third_scenario():
print "You two get out of the cell and approach to the exit, a long corridor is ahead of you, flashing red lights indicate an state of emergency, you must evacuate."
print "As soon as you two approach to the door, it closes"
print "Crap it must be the emergency system, we have been detected"
next = raw_input("What do you do> ")
if next == 'Examine the door':
print "A small panel comes out, requires to enter a code of words"
third_scenario_code()
elif next == 'Try to force it':
print "You try to force the door with no result"
print options_thirdscenario_actions
next2 = raw_input("What else do you do> " )
if next2 = 'Examine the door'
third_scenario_code()
else:
print "You already did that"
I am getting a similar error on the whole program and I suspect it has something to do with indentation, but I have tried every suggestion I see in google with no fruitful result. thanks in advance.
You are missing colons after the one of if conditions and need to line things that are the same scope up, i.e. the print after the function call but you may also be mixing spaces and tabs. It is recommended to always use 4 spaces rather than tabs and most programming editors can be set up for this.
I would also suggest getting hold of pylint and using it. It will help you spot a lot of potential errors and will help you to develop good habits.
Its because of the indentation of third_scenario_code() you need to write it under the print .
change the following :
if next == 'Examine the door':
print "A small panel comes out, requires to enter a code of words"
third_scenario_code()
to :
if next == 'Examine the door':
print "A small panel comes out, requires to enter a code of words"
third_scenario_code()
I'm trying to learn how to program and I'm running into a problem....
I'm trying to figure out how to make sure someone inputs a number instead of a string. Some related answers I found were confusing and some of the code didn't work for me. I think someone posted the try: function, but it didn't work, so maybe I need to import a library?
Here's what I'm trying right now:
Code:
print "Hi there! Please enter a number :)"
numb = raw_input("> ")
if numb != str()
not_a_string = int(next)
else:
print "i said a number, not a string!!!"
if not_a_string > 1000:
print "You typed in a large number!"
else:
print "You typed in a smaller number!"
Also I have another question while I'm asking. How can I make it so it will accept both uppercase and lower case spellings? In my code below, if I were to type in "Go to the mall" but with a lowercase G it would not run the if statement because it only accepts the capital G.
print "What would you like to do: \n Go to the mall \n Get lunch \n Go to sleep"
answer = raw_input("> ")
if answer == "Go to the mall":
print "Awesome! Let's go!"
elif answer == "Get lunch":
print "Great, let's eat!"
elif answer == "Go to sleep":
print "Time to nap!"
else:
print "Not what I had in mind...."
Thanks. ^^
Edit: I'm also using python 2.7 not 3.0
You can do something like this:
while True: #infinite loop
ipt = raw_input(' Enter a number: ')
try:
ipt = int(ipt)
break #got an integer -- break from this infinite loop.
except ValueError: #uh-oh, didn't get an integer, better try again.
print ("integers are numbers ... didn't you know? Try again ...")
To answer your second question, use the .lower() string method:
if answer.lower() == "this is a lower case string":
#do something
You can make your string comparisons really robust if you want to:
if answer.lower().split() == "this is a lower case string".split():
In this case, you'll even match strings like "ThIs IS A lower Case\tString". To get even more liberal in what you accept, you'd need to use a regular expression.
(and all this code will work just fine on python2.x or 3.x -- I usually enclose my print statements in parenthesis to make it work for either version).
EDIT
This code won't quite work on python3.x -- in python3, you need to change raw_input into input to make it work. (Sorry, forgot about that one).
First,you should ask only one question per post.
Q1: use built-in .isdigit()
if(numb.isdigit()):
#do the digit staff
Q2:you can use string.lower(s) to solve the capital issue.
you may try
numb = numb.strip()
if numb.isdigit() or (numb[0] in ('+', '-') and numb[1:].isdigit():
# process numb