+ 1

Intermediate Python - 31.2 Practice help - “Say something”

You are making a program to post tweets. Each tweet must not exceed 42 characters. Complete the program to raise an exception, in case the length of the tweet is more than 42 characters. ————————————— Getting errors on test case 3 and 4. Not sure what to do. Please help, thanks! Code: tweet = input() try: num = 42 if num > 42: raise ValueError except: print("Error") else: print("Posted")

28th Jul 2021, 8:44 PM
Zach Z
7 Antworten
+ 4
tweet = input() try: #your code goes here if len(tweet) > 42: raise ValueError except: print("Error") else: print("Posted")
18th Feb 2022, 10:33 AM
Liria
Liria - avatar
+ 2
You rock. Thanks! ✩✮★✮✩
28th Jul 2021, 8:48 PM
Zach Z
+ 1
I did a shorter version, using the len function, with the simple if and else, and removed the try and Errors. Works for all the tests, though it may not be the solution intended by the Code Coach makers! tweet = input() #your code goes here num = len(tweet) if num > 42: print("Error") else: print("Posted")
2nd Nov 2021, 2:35 PM
Rafique
Rafique - avatar
0
tweet = input() try: #your code goes here if len(tweet) >= 42: raise ValueError except: print("Error") else: print("Posted")
3rd Jan 2022, 2:38 PM
Nazarii Zavada
Nazarii Zavada - avatar
0
My solution: tweet = input() try: #your code goes here if len(tweet) > 42: raise Exception() except: print("Error") else: print("Posted")
10th Nov 2022, 8:38 AM
Pooja Patel
Pooja Patel - avatar
0
tweet = input() try: #your code goes here if len(tweet)>42: raise Exception() except: print("Error") else: print("Posted")
30th Mar 2023, 10:56 PM
المهندس أحمد
المهندس أحمد - avatar
0
hi ! Maybe my code can be worked for you! tweet = input() #my code is here try: if len(tweet)>42: raise ValueError("tweet is more than 42 characters") except: print("Error") else: print("Posted")
2nd Dec 2023, 8:11 AM
vahid kamrani
vahid kamrani - avatar