• Register
1 vote
2.8k views

Problem:

I have a piece of code in python and I want to print a list of integer in ASCII. The problem comes once I hit the run button. I don’t know what wrong goes with it. It throws back an error: typeerror: an integer is required (got type str)

n=list(str(12345))
for q in n:
    print(chr(q))

Here is my code snippet. Where I am doing the wrong things? Could you please figure it out?

Thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution:

You have declared a list with string type and passing it to char() function. You need to cast it to integer type while passing the value through the char() function.

Like this:

n=list(str(12345))
for q in n:
    print(chr(int(q)))

Now, this looks like what you were wanting. Give it a try and post your feedback here.

Thanks.

13 9 6
94,240 points
0 votes

Solution:

You are passing a string value into the chr() function. This must work:

a=list(str(12345))
for q in a:
    print(chr(int(q)))

#The above code will work but this will print out characters, as 1-5
# in the ASCII table are not visible characters.

a = [65,66,67,68,69]
for q in a:
    print(chr(q))

At the time you did x = F.relu(self.lstm(inputs)) in your forward pass, you forgot to give the lstm with hidden and cell units.

10 6 4
31,120 points

Related questions

0 votes
2 answers 1.2K views
1.2K views
Problem : I am new to Py 3.5. This code was working properly in Python 2.7: with open(fname, 'rb') as f: lines = [x.strip() for x in f.readlines()] for line in lines: tmp = line.strip().lower() if 'some-pattern' in tmp: continue # ... code After upgrading to 3.5, I'm getting the: TypeError: a bytes-like object is required, not 'str'
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 12 views
12 views
Problem: typeerror: unsupported operand type(s) for +: 'nonetype' and 'str'
asked Feb 15 Ethan ross 1.8k points
0 votes
1 answer 25 views
25 views
Problem: Hi! I am new to the world of programming. I am trying to multiply two numbers that will be entered by the user. I wrote the following program: number1 = input("Enter first number: ") number2 = input("Enter second number: ") print(number1," * ",number2," = ", ... = ",number1*number2) TypeError: can't multiply sequence by non-int of type 'str' I have no idea what I am doing wrong. Any help?
asked Dec 21, 2020 Code Learner 6k points
1 vote
1 answer 70 views
70 views
Problem: I wrote the following program in python and it throws me the error message: Please enter your first number: 10 Please enter your secoend number: 5 Addition of your numbers 105 Traceback (most recent call last): File "ires.py", line 9, in <module> ... some wong output and then it throws the error message. Is there any problem in my program thats causing this error? Thanks in advance.
asked May 12, 2020 samhaz 5k points
1 vote
1 answer 170 views
170 views
Problem: Hi all This is a question from a noob who started coding a few hours back. print("Hello") print("name") print("hello") print("Let's do some math!") print(5 + "a") print("5 + 5") print("Thanks for playing along!") It says, &ldquo;typeerror: unsupported operand type(s) for +: 'int' and 'str'&rdquo; What does it mean?
asked Mar 9, 2020 Gavin 15.3k points
0 votes
1 answer 52 views
52 views
Problem: Hi there! I am a noob and I have got an assignment in which I have to take two numbers as input from users and print the even numbers that lie between two numbers. I wrote the following program: starting = input("Enter the starting number: ") ending = ... : TypeError: 'str' object cannot be interpreted as an integer I have no idea what is wrong with my program. Can anybody here help me?
asked Dec 21, 2020 Code Learner 6k points
0 votes
1 answer 16 views
16 views
Problem: I am trying to connect to a server using a socket module in Python. However, I am getting an error like below. TypeError: a bytes-like object is required, not 'str' Here is my code: import socket HOST = '0.0.0.0' PORT = 12345 server_socket = socket.socket ... is causing this TypeError? and what should I do to fix it? I'm also open to any advice on how to improve your Python socket coding.
asked Feb 7 sasha 13.2k points
0 votes
1 answer 12 views
12 views
Problem: I've very recently migrated to Py 3.5. This code was working properly in Python 2.7: with open(fname, 'rb') as f: lines = [x.strip() for x in f.readlines()] for line in lines: tmp = line.strip().lower() if 'some-pattern' in tmp: continue # ... code After upgrading to 3.5, I'm getting the: TypeError: a bytes-like object is required, not 'str'
asked Jan 21 Mashhoodch 9.6k points