Day 031 - Project 31 : f-string User Interfaces

If you have any questions, comments or issues with this project please post them here!

I went retro :mirror_ball: and recreated two classic user interfaces. Can you guess what they are?

https://replit.com/@JackAdem/Day-031-Project-31-f-string-User-Interfaces?v=1

Day 31 of #Replit100DaysOfCode #100DaysOfCode.

1 Like

Question: Why is this printing to 2 new lines? How do I make Armbook appear on the line after weclome to?

Repl link: main.py - Day31_100 days - Replit

#ARMBOOK
print("\033[1;0m")
print("\n\n\n\n\n\n\n")
welcome="WELCOME TO"
armbook="--  ARMBOOK  --"
welcome1=f"{welcome:^70}"
print(welcome1)
armbook1=f"{armbook:^70}"
print("\033[1;34m",armbook1)

@DukeTI please avoid pinging people not already involved in posts.

The reason your code is printing two lines is because print will add a new line to the end of the text by default. If you’d like it to not do this, add end="" to the print statement.

For example:

print("A")
print("B")

Outputs:

A
B

However:

print("A", end="")
print("B")

Outputs:

AB
2 Likes

The solution presented by Firepup seems right to me. Let us know if it doesn’t work. Alternatively, you could put both lines in one print statement, like this:

#ARMBOOK
print("\033[1;0m")
print("\n\n\n\n\n\n\n")
welcome="WELCOME TO"
armbook="--  ARMBOOK  --"
welcome1=f"{welcome:^70}"
armbook1=f"{armbook:^70}"
print(welcome1 + "\n" +  "\033[1;34m" + armbook1)
2 Likes

Unsure why my code to shift the text “queen” to the right is not working. Shouldn’t appending : >5 to the text string variable shift it right 5 spaces? However, it is hard left ruled. I can fix it if I just manually insert 5 spaces but this is inelegant and I want to understand why this isn’t working. Code below, thanks. Haven’t watched the solution video yet, trying to avoid that if at all possible.

def red(text):
  return "\033[0;31m" + text + "\033[0m"


def blue(text):
  return "\033[0;34m" + text + "\033[0m"


def green(text):
  return "\033[0;32m" + text + "\033[0m"


def purple(text):
  return "\033[0;35m" + text + "\033[0m"


def yellow(text):
  return "\033[1;33m" + text + "\033[0m"


text = f"{red('=')}{'='}{blue('=')}{yellow(' Music App ')}{blue('=')}{'='}{red('=')}"
text2 = f"{yellow('Queen')}"
width = 80
print(f"{text.center(width)}", end="\n")
print("🔥▶️  Radio Gaga", end="\n")
print(f"{text2: >5}", end="\n\n")

no shifting will be done because text2 already takes up more than 5 chars

2 Likes

Thanks! changing the 5 to 21 to account for the 16 chars in text2 has resolved the issue.

1 Like

Hello. Need some help. Trying to do as what David instructed me to do. But all I got was “None” when trying to print the results:

def Colors(color, word):
  if color == "yellow":
    print("\033[1;33m" + word + "\033[0m")
  elif color == "green":
    print("\033[0;32m",word,"\033[0m'")
  elif color == "purple":
    print("\033[35m",word,"\033[0m")
  elif color == "blue":
    print("\033[34m",word,"\033[0m")
  elif color == "red":
    print("\033[31m",word,"\033[0m")
  else:
    print("\033[30m",word,"\033[0m")

Colors("red", "Hello")
Colors("stop","Back to normal")
Colors("green","Welcome to Day 31")
Colors("nope", "keep going")
Colors("purple","I'm happy")

Title1 = f"{Colors('yellow','Music App')}"

Radio_Gaga = f"{Colors('white','Radio Gaga')}"
Song = f"{Colors('white','Queen')}"

Prev = f"{Colors('white','PREV')}"
Next = f"{Colors('green','NEXT')}"
Pause = f"{Colors('purple','PAUSE')}"

Title2 = f"{Colors('white','WELCOME TO')}"
Name = f"{Colors('blue','ARMBOOK')}"


Sentence = f"{Colors('yellow','Definitely not a rip off of a certain other social networking site.')}"

Honest = f"{Colors('red','Honest.')}"

Username = f"{Colors('white','Username:')}"
Password = f"{Colors('white','Password:')}"

print(f"{Title2: ^50}")
print(f"{Name: ^50}")

print(f"{Sentence: ^100}")
print()
print(f"{Honest: ^50}")
print()
print(f"{Username: ^50}")
print(f"{Password: ^50}")

Hello,
in this function you never return a string. Do you see a single return statement here? When a python function returns without an explicit return value, it actually returns None. For example, the print() function returns None. Your Colors() function also always returns None.
To fix this, when you are using your Colors() function, have the print statements either only in the function, or only in the calling code. Depending on which way you go, you’ll either return the string, or ignore the return value.

3 Likes

Thank you for your suggestion.

It no longer “None” after I included the return function in the subroutine.

But the colors for the aligned variables still doesn’t displayed in the console yet.

This is because of how you are printing it.

print("\033[1;33m" + word + "\033[0m")  # concatenation: correct
print("\033[0;32m",word,"\033[0m")  # wrong
1 Like

Ah ok. I did that. But the color is still not visible in the console yet.

That’s effectively the exact same code, aside from like 2 spaces.

2 Likes

Hmm.
Well, I’m not sure then. Hopefully another member of the community can help you here.

1 Like