These are the steps given by my professor,:
Store this input as variable 'tag'
Calculator half the length of the string_length, rounded down
Store this as 'first_half'
Calculate a new string that is the concatenation of 'tag' and 'first_half' in the following format first_half
Attached is the code I've already run. Everything works fine up until I start with the "tag" variable being defined. So basically around step 9 and onward I have issues.
user_input=input ("Please enter something")
string_length=len(user_input)
even_message= string_length % 2
if even_message>0:
print ("The length is", string_length, "characters long and is
odd.")
else:
print ("Your length is", string_length, "characters long and is
even.")
tag= input("Input one of the following: h1, div or article.")
midpoint = (string_length/2)
first_half= (midpoint)
tagged_input= print ((tag) + (midpoint) + "/" + (tag))
Output should be this. (uploaded a screenshot to imgur)
this should work(replace the last 3 lines of your code with this):
midpoint = (string_length//2)
first_half = user_input[:midpoint]
tagged_input = f"<{tag}>{first_half}</{tag}>"
(1) the first line uses the // operator, that divides and rounds down
(2) the second line uses list/sequence slicing: Understanding slice notation
(3) the third line uses a fairly new and very useful feature of python called f-strings: https://realpython.com/python-f-strings/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.