I want a space on the Farhan Sadik and Snigdho
I tried this many times on spacing Name, Name2, Welcome but there is no result.
Actually you concatenante your strings with no space between them.
You can do:
print("Hi", name1, name2, welcome)
or:
print("Hi " + name1 + " " + name2 + " " + welcome)
You can simply solve this, adding a space:
Name + " " + Name2 + " " + third_variable
print(Name + " " + Name2 + " " + "WELCOME")
OUTPUT:
Value of Name value of name2 WELCOME
If comma(,) is used in the place of " " it will also give the same output (as above)
The above code prints the value of name and a space (" ") and again name2 and a space (" ") and the string "WELCOME".
In the same way if you use \n in the space of " " you will get the output of the string or variable next to the \n in a new line. For example:
print(Name + "\n" + Name2 + "\n" + "WELCOME")
Output:
Value of Name
Value of Name2
WELCOME
In the same way if \t is used in the place of \n it will print the variable or string next to it after a tab space, like:
Output:
Value of Name Value of Name2 WELCOME
Related
this is my first question.
I've been working in this assignment in which I had to do a Notepad, and then add a lexical analyzer function in it. The goal was to write code in the notepad and then use the lexical analyzer to break it up and categorize it; and for the last part, it had to change the tokenized words categorized as "Identifiers" by Id and the number of Id that it is, and lastly print the code again with this change.
I've achieved almost everything, but this last parto of changing the tokenized words has beeen difficult to me.
`
def cmdAnalyze ():
Analyze_program = notepad.get(0.0, END)
Analyze_program_tokens = nltk.wordpunct_tokenize(Analyze_program);
RE_keywords = "auto|break|case|char|const|continue|default|print"
RE_Operators = "(\++)|(-)|(=)|(\*)|(/)|(%)|(--)|(<=)|(>=)"
RE_Numerals = "^(\d+)$"
RE_Especial_Character = "[\[#&!#$\^\|{}\]:;<>?,\.']|\(\)|\(|\)|{}|\[\]|\""
RE_Identificadores = "^[a-zA-Z_]+[a-zA-Z0-9_]*"
RE_Headers = "([a-zA-Z]+\.[h])"
# Categorización de tokens
notepad.insert(END, "\n ")
for token in Analyze_program_tokens:
if (re.findall(RE_keywords, token)):
notepad.insert(END, "\n " + token + " --------> Palabra clave")
elif (re.findall(RE_Operators, token)):
notepad.insert(END, "\n " + token + " --------> Operador")
elif (re.findall(RE_Numerals, token)):
notepad.insert(END, "\n " + token + " --------> Número")
elif (re.findall(RE_Especial_Character, token)):
notepad.insert(END, "\n " + token + " --------> Carácter especial/Símbolo")
elif (re.findall(RE_Identificadores, token)):
notepad.insert(END, "\n " + token + " --------> Identificadores")
elif (re.findall(RE_Headers, token)):
notepad.insert(END, "\n " + token + " --------> Headers")
else:
notepad.insert(END, "\n " + " Valor desconocido")
notepad.insert(END, "\n ")
notepad.insert(END, Analyze_program_tokens)
This is my current output:
>>> print(‘Hello World’)
>>> --------> Carácter especial/Símbolo
print --------> Palabra clave
(‘ --------> Carácter especial/Símbolo
Hello --------> Identificadores
World --------> Identificadores
’) --------> Carácter especial/Símbolo
>>> print (‘ Hello World ’)
`
The last line output has to be like this: ">>> print (‘ Id1 Id2 ’)"
Thank you for reading :)
I would add
id_count = 0
just before the for loop, and then modify the handling of identifiers like this:
elif (re.findall(RE_Identificadores, token)):
id_count += 1
notepad.insert(END, "\n " + f"Id{id_count:02d}' + " --------> Identificadores")
EDIT
On second thoughts, what happens if an identifier occurs more than once in the notepad? "hello, hello world" should result in "Id01, Id01 Id02" or in "Id01, Id02 Id03"?
In the first case you will need a dictionary. So before the for loop let's also add
ids = {}
and use the dictionary like this
elif (re.findall(RE_Identificadores, token)):
if token not in ids:
id_count += 1
ids[token] = id_count
notepad.insert(END, "\n " + f"Id{ids[token]:02d}' + " --------> Identificadores")
I keep editing my text but I keep on getting the same error!
My code:
import random
Female_Characters = ["Emily", "Ariel", "Jade", "Summer"]
Male_Characters = ["Blake", "Max", "Jack", "Cole", "Daniel"]
PlacesToMeet = ["Beach", "Park", "Train Station", "Cave"]
SheSaid = ["Lets go explore!", "I'm tired", "I saw a purple frog", "My tounge hurts"]
HeSaid = ["I didnt get much sleep", "I wanna go inside that cave!", "Oh, ok"]
Outcomes = ["They never got to", "They were never found again.", "They enjoyed their day and went to
get Ice Cream!"]
ChosenFemale = random.choice(Female_Characters)
ChosenMale = random.choice(Male_Characters)
ChosenMeet = random.choice(PlacesToMeet)
ChosenShesaid = random.choice(SheSaid)
ChosenHeSaid = random.choice(HeSaid)
print ("There were two friends, their names are ") + (ChosenMale) + (", and ") + (ChosenFemale) + (".") + ("One day when they were at the") + (ChosenMeet) + (", ") + (ChosenFemale) + (" Said, ") + (ChosenShesaid) + (". Then") + (ChosenMale) + (" Said ") + (ChosenHeSaid) + (". After that, ") + random.choice(Outcomes)
Still new to python
You have incorrect parentheses on the print line.
print("There were two friends, their names are " + ChosenMale + ", and " + ChosenFemale + "." + "One day when they were at the" + ChosenMeet + ", " + ChosenFemale + " Said, " + ChosenShesaid + ". Then" + ChosenMale + " Said " + ChosenHeSaid + ". After that, " + random.choiceOutcomes)
Your code was calling
print("There were two friends, their names are ")
which returns None, and then trying to concatenate that with all the other strings.
Maybe you were following Python 2.x instructions. In Python 2, print is a statement so it didn't take an argument in parentheses, but in Python 3 it's an ordinary function.
remove the parenthesis at the end of this string
print("There were two friends, their names are "
and add it at the end of
random.choice(Outcomes)
so it should be:
print ("There were two friends, their names are " + (ChosenMale) + (", and ") + (ChosenFemale) + (".") + ("One day when they were at the") + (ChosenMeet) + (", ") + (ChosenFemale) + (" Said, ") + (ChosenShesaid) + (". Then") + (ChosenMale) + (" Said ") + (ChosenHeSaid) + (". After that, ") + random.choice(Outcomes))
I am writing a program that keeps track of the animals on a farm and I want to be able to search for an animal by e.g: name, gender, age etc, which all are attributes to the objects. I am a complete noob when it comes to Python so all help is very appreciated.
Here is the code i have so far for this, but it only ads the attribute that is searched for to the list and then prints it. I want to be able to add the entire object to the list and print the whole object through that list.
class Djur:
def __init__(self, art, namn, ålder, kön, gravid):
self.art = art
self.namn = namn
self.age = ålder
self.gender = kön
self.gravid = gravid
def __str__(self):
return ("Art: " + str(self.art) + " " + "\n"
"Namn: " + str(self.namn) + " " + "\n"
"Ålder: " + str(self.age) + " " + "\n"
"Kön: " + str(self.gender) + " " + "\n"
"Gravid: " + str(self.gravid))
def __repr__(self):
return str(self)
try:
val2 = input("Ange sök-text")
l=[]
for x in djurlista:
y = x.art or x.gender or x.namn or x.gravid or x.age
if y == val2:
l.append(x.art)
print(l)
meny()
except ValueError:
print("Var god välj ett giltigt alternativ!")
To create a list with the objects you just need to write l.append(x) instead of l.append(x.art). Now you are just appending the property art.
This question already has an answer here:
How can I concatenate str and int objects?
(1 answer)
Closed 6 years ago.
i'm trying to print the operation however the first statement is working but second statement has error.
conSwitch = raw_input("Enter cycle of context switch: ")
cycleOpr = raw_input("Enter cycle operation: ")
totalCycle = float(conSwitch) + float(cycleOpr) + float(conSwitch)
print "Context =", conSwitch
print "Cycle operation =", cycleOpr
print "Context switch back =", conSwitch
print("\n")
print (conSwitch + " + " + cycleOpr + " + " + conSwitch)
print ("Total number of cycle is: " + format(totalCycle))
print("===================================================")
reqSecond = raw_input("Enter cycle request second:")
print "Total cycle request =", totalCycle
print "Cycle request per second =", reqSecond
print("\n")
totalSpent = float(totalCycle) * float(reqSecond)
print (totalCycle + " * " + reqSecond)
print ("Total number of spent = " + format(totalSpent))
==============================================================
First statement
Work===>> print (conSwitch + " + " + cycleOpr + " + " + conSwitch)
Second statement
Error===>> print (totalCycle + " * " + reqSecond)
The problem here is that the variable totalCycle is of type float. Python does not know what it means to do + between a type float and string (because " * " is a string).
To do it the way you showed you have to convert totalCycle to string first, like this:
print (str(totalCycle) + " * " + reqSecond)
FORMAT Syntax
"First, thou shalt count to {0}" # References first positional argument
"Bring me a {}" # Implicitly references the first positional argument
"From {} to {}" # Same as "From {0} to {1}"
"My quest is {name}" # References keyword argument 'name'
"Weight in tons {0.weight}" # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}" # First element of keyword argument 'players'.
ok so I a trying to make a calculator in python and it comes up with ParseError: Bad Input on line 7 and ParseError: Bad Input on line 6 etc. all the way down to ParseError: Bad Input on line 1, can anyone spot the error and how to fix it.
1:) n = input(" select first number: ")
2:) d = raw_input("What operation: ")
3:) print " What operation: " + str(d)
4:) n1 = input(" select second number ")
6:) if d == "+":
7:) print "Did you know that " + str(n) + " plus " + str(n1) + " is "
7:) + str(n+n1)+ "?"
8:)
9:)print " "
10:)print "Goodbye"
The line:
print "Did you know that " + str(n) + " plus " + str(n1) + " is "
will happily print something. Then the interpreter sees this:
+ str(n+n1)+ "?"
and has no idea what you mean, because it doesn't know you're continuing the previous line's print statement. You can fix this by adding parentheses:
>>> print ("Did you know that " + str(1) + " plus " + str(1) + " is "
... + str(2)+ "?")
Did you know that 1 plus 1 is 2?
Now the interpreter knows that, when you finish that first line, you're not done entering the expression. It will wait for you to finish a valid statement before processing it. See also logical lines and physical lines in Python.