Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+3 votes

How can I make a string statement be considered an integer and use it in a mathematical equation? For example if I have 'Anakin 87' I would like to take just the '87' and use it to compute something.

asked in CSC201 Spring 2021 by (1 point)

1 Answer

+3 votes

You could use .split() to separate the name from the number, and then convert the number to an integer with int(). Something like

number = int(variable[1])

The variable[1] is because if you split "Anakin" and "87", 87 is the second item in the list, so it gas index [1].


answered by (1 point)

I have done that but somehow I'm getting a error :(

grade = int(split[1])

ValueError: invalid literal for int() with base 10: 'Helvetica;}'

This is what Thonny tells me once I run it


It sounds like a non-number string is being put into the int. What is the vriable 'split' equal to in your code?