• Register
1 vote
1.1k views

Problem:

I  made a tuple to add to my main program . But this thing does not seem to work . I wrote this code:
 

print("You should study hard to pass the exam")
exam=("Study","Pc","python")
print ("The things I have :")
for stuff in exam:
    print (stuff)
print("I still need my previous lectures")
extra=("Lectures")
exam += extra
for stuff in exam:
    print (stuff)

 

But after adding this or after running this  got the error message:
 

You should study hard to pass the exam
The things I have :
Study
Pc
python
I still need my previous lectures
Traceback (most recent call last):
  File "main.py", line 8, in <module>
    exam += extra
TypeError: can only concatenate tuple (not "str") to tuple

Can anyone sort out this problem and help me out to solve this?

 

8 6 2
4,980 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

Tuples are constructed by the comma operator (not within square brackets), with or without enclosing parentheses, but an empty tuple must have the enclosing parentheses, such as a, b, c or (). A single item tuple must have a trailing comma, such as (d,), so if you do this to your code it should work

extra = "Lectures", 
or

extra = ("Lectures",)

and your code should be like:

print("You should study hard to pass the exam")
exam=("Study","Pc","python")
print ("The things I have :")
for stuff in exam:
    print (stuff)
print("I still need my previous lectures")
extra=("Lectures",)
exam += extra
for stuff in exam:
    print (stuff)

 

And its output will be:

You should study hard to pass the exam
The things I have :
Study
Pc
python
I still need my previous lectures
Study
Pc
python
Lectures

Hope you understand. 

Happy coding.

11 6 4
34,950 points

Related questions

0 votes
1 answer 110 views
0 votes
1 answer 212 views
0 votes
1 answer 21 views
21 views
Problem: I am providing two values (mon_voltage and core_voltage) via the command line and I need to find a "starting point" from a two dimensional array where my iteration or loop will start from. Here is my code: myArray = [[1.02,1.13],[1.02,1.16],[1.02,1. ... : can only concatenate tuple (not "int") to tuple I can't figure out why start_point, which is an index, is a tuple. Please help.
asked Feb 13 sasha 13.2k points
1 vote
1 answer 700 views
700 views
Problem: I have a thousand line codes of python that I can not attach here. I am attaching the infected part from my whole code. This portion of code is producing the error. can only concatenate tuple (not "int") to tuple def calculateAns(num1, num2, ans, right): if ans ... ' right = right + 1 else: print 'Wrong' return right, ans I am looking forward to you guys to have a legit solution. Thanks.
asked Mar 30, 2020 Gavin 15.3k points
0 votes
1 answer 24 views
24 views
Problem: I am new to the world of programming . I recently started learning Python. I wrote following program: def check(list): for i in range(len(list)): list[i] = list[i+1] value = list[i] print(value) input_list = eval(input("Enter the items of list")) check( ... to replace the first item of the list with the second, and so on. I have no idea why this error is occurring. Can anybody help me?
asked Dec 27, 2020 Code Learner 6.3k points
1 vote
1 answer 79 views
79 views
Problem: I am a new student in Python programming. I have made a program that is supposed to take multiple inputs from the user and then print simply. Yeah, I know that sounds pretty easy but I am facing some kind of TypeError from the program. I am attaching the ... am getting is: TypeError: can only concatenate str (not "int") to str What&rsquo;s wrong here? Thanks for your kind help, mate.
asked Jun 29, 2020 adamSw 11.3k points
1 vote
2 answers 920 views
920 views
Problem: I have a very simple question for you guys. What does the error: can only concatenate list (not "int") to list mean? Well, let me clarify my problem a little bit more. I am trying to add to lists in python, but it&rsquo;s not working. Yeah, I am a new ... my snippet number=[1, 2, 3] number[1:]+number[0] Where did I wrong in my program? Your kind solution is much appreciated. Thanks mate!
asked Mar 30, 2020 Gavin 15.3k points
1 vote
1 answer 427 views
427 views
Problem: I wrote a program in which gives me the following error : TypeError: startswith first arg must be bytes or a tuple of bytes, not str The program I wrote is: def allFasta(filename): xd = open(filename, 'yb') headers = "" sequence = "" while True: ... ("youn.xd") Why is this type error message is appearing for this program. Is there any problem with my programs arguments? Thanks in advance.
asked Jun 11, 2020 samhaz 5k points
0 votes
1 answer 33 views
33 views
Problem: Hi there! I just recently learned loops in Python that are a bit complex and tricky. I got an assignment from my teacher in which I have to write a program that takes a number as input from the user and prints the multiplication table of that number. I tried ... that the user does not enter a negative number. I do not know what the problem is and how to resolve it. Any help? Thanks.
asked Dec 2, 2020 Code Learner 6.3k points
1 vote
1 answer 37 views
37 views
Problem: I have a program where I used tuple and it is throwing e some eror message. The code I worte is : import random board = [] for x in range(0,5): board.append(["O"] * 5) def print_board(board): for row in board: print " ".join(row) print_board(board) ... len(board)-1 or guess_col < 0 or guess_col > len(board[0])-1: TypeError: list indices must be integers not tuple How to solve this thing?
asked Apr 27, 2020 samhaz 5k points