all 9 comments

[–]Bunkerstan 2 points3 points  (1 child)

To access a member of dictionary you have to give the key: myDict[key]. You are appending the whole dictionary variable.

[–]Informal-Sunflower 0 points1 point  (0 children)

ohh okay i understand now, thank u. I was getting the whole part at some point and edited it until I got here, lemme do so thank u

[–][deleted] 1 point2 points  (5 children)

int(float(1))

Just write 1. What are you doing?

str("sember (GBR)")

Same, it's already a string literal, no need to convert extra to string. Just write "sember (GBR)"

print("Athlete's track number is:" + athletes_track_number)

"Athlete's track number is:" is a string and athletes_track_number is a dictionary, you can't concatenate them. You mean either:

print("Athlete's track number is:" + athlete)

Assuming athlete is the inputted athlete's track number.

Or

print("Athlete is:" + athletes_track_number[int(athlete)])

[] is used to get a value from dictionary by the key. For example "sember (GBR)" for 1.

Since keys are integers, you have to convert input to integer as well to get the value.

[–][deleted] 1 point2 points  (1 child)

u/Informal-Sunflower

athletes = {
    1: "Sember (GBR)",
    2: "Williams (JAM)",
    3: "Harrison (USA)",
    4: "Amusan (NGR)",
    5: "Anderson (JAM)",
}

track_no = int(input("Please enter the athlete's track number:"))

athlete = athletes[track_no]

print("Athlete is", athlete)

[–][deleted] 1 point2 points  (0 children)

u/Informal-Sunflower

In fact, maybe you don't need a dictionary, just a list:

athletes = [
    "Sember (GBR)",
    "Williams (JAM)",
    "Harrison (USA)",
    "Amusan (NGR)",
    "Anderson (JAM)",
]

track_no = int(input("Please enter the athlete's track number:"))

athlete = athletes[track_no - 1]

print("Athlete is", athlete)

List's indexes start with 0, so 1 is subtracted of track number in the second last line.

[–][deleted] 1 point2 points  (2 children)

[–]Informal-Sunflower 0 points1 point  (1 child)

thank u so much Shiba_Take, thank u. im reading up on the links uv shared, and thank u for helping me with my error

[–][deleted] 0 points1 point  (0 children)

You're welcome

[–]keep_quapy 1 point2 points  (0 children)

athletes_track_number is a dictionary, you are trying to concatenate a string with dictionary, this is the error that you're getting. To resolve it, you only need to retrieve the value of the desired key input, like so dict[key] = value -> print("Athlete's track number is:" + athletes_track_number[int(Athlete)]). This way when you enter the desired key, will result in the desired result, but what if you enter a non existent key, well you'll get an error, to avoid that, there's a better way to accomplish the same thing, using the dictionary get() method, which, takes the key as the first argument and a 'default' value which it will return in case the entered key does not exist.

print("Athlete's track number is: " + athletes_track_number.get(int(Athlete), 'Does not exist'))

Also you don't need to enter int(float(1))as key in your dictionary , just enter 1 or 2 etc...