+ 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")
7 odpowiedzi
+ 4
tweet = input()
try:
#your code goes here
if len(tweet) > 42:
raise ValueError
except:
print("Error")
else:
print("Posted")
+ 2
You rock. Thanks! ✩✮★✮✩
+ 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")
0
tweet = input()
try:
#your code goes here
if len(tweet) >= 42:
raise ValueError
except:
print("Error")
else:
print("Posted")
0
My solution:
tweet = input()
try:
#your code goes here
if len(tweet) > 42:
raise Exception()
except:
print("Error")
else:
print("Posted")
0
tweet = input()
try:
#your code goes here
if len(tweet)>42:
raise Exception()
except:
print("Error")
else:
print("Posted")
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")