rage / programming-24

43 stars 28 forks source link

Help #58

Open Aprialmay opened 1 month ago

Aprialmay commented 1 month ago

I'm am programming beginner I do not know what is the problem about my programs https://tmc.mooc.fi/paste/P9pRaa6UaebbGClFGCCWUA

Yellecode commented 1 month ago

Hello Aprialmay,

First of all, I am nowhere near the exprience of a teacher, but for this one I feel comfortable to provide some knowledge. I think this exercise is broken at the moment. Whatever you submit even correctly, it fails.

That being said, it doesn't mean we cannot improve your code.

Your code:

given_name=input("given name:")
family_name=input("family name:")
street_address=input("street address:")
city=input("city:")
postal_code=input(" postal code:")
#Print the information in the specified format
print(f"\n{given_name Steve}{family_name Sanders}")
print(f"\n{street_address 91 Station Road}" )
print(f"\n{city London}{postal_code EC05 6AW}")

The prompts for user input are fine, but it's generally good practice to make them more readable by capitalizing the first letter and maintaining consistent spacing. For example, use "Given name: " instead of "given name:".

Incorrect Variable Usage in Print Statements: The print statements are attempting to mix variables and hardcoded text incorrectly.

For example:

print(f"\n{given_name Steve}{family_name Sanders}")

This line attempts to concatenate the variable given_name directly with the text "Steve" and the variable family_name directly with "Sanders". This is not how variables and strings should be combined.

Hardcoded Values: The provided print statements use hardcoded values (e.g., "Steve", "Sanders", "91 Station Road", "London", "EC05 6AW"). Instead, we should use the variables that store the user inputs.

Corrected Code:

# Ask for the user's name and address

given_name = input("Given name: ")
family_name = input("Family name: ")
street_address = input("Street address: ")
city = input("City: ")
postal_code = input("Postal code: ")

Print the information in the specified format

print(f"\nGiven name: {given_name}")
print(f"Family name: {family_name}")
print(f"Street address: {street_address}")
print(f"City and postal code: {city} {postal_code}")

# Final output as required

print()
print(f"{given_name} {family_name}")
print(f"{street_address}")
print(f"{city} {postal_code}")

We ask the user for their given name, family name, street address, city, and postal code, and store these inputs in variables:

given_name = input("Given name: ")
family_name = input("Family name: ")
street_address = input("Street address: ")
city = input("City: ")
postal_code = input("Postal code: ")

We correctly use f-strings to include the values of the variables in our output:

print(f"\nGiven name: {given_name}")
print(f"Family name: {family_name}")
print(f"Street address: {street_address}")
print(f"City and postal code: {city} {postal_code}")

We print the name and address in the final required format:

print()
print(f"{given_name} {family_name}")
print(f"{street_address}")
print(f"{city} {postal_code}")

Summary: Input: We gather user input correctly. Output: We use the variables to print the collected information in the specified format without hardcoding any values. Formatting: We ensure that the prompts and outputs are formatted cleanly and consistently.

This approach ensures the program is flexible and correctly displays the user's input.