Help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
49 Answers
0
Podle Playz No, I mean you wrap the code that generates a letter into one function, and the code generating a vowel into another, and call them several times to construct a word. ``` def get_letter(): alphabet = (random.randint(1, 26)) if alphabet == 1: alphabet = "a" ... elif alphabet == 26: alphabet = "z" def get_vowel(): vowel = (random.randint(1, 5)) if vowel == 1: vowel = "a" ... elif vowel == 5: vowel = "u" print("Bot said: " + gen_letter() + gen_vowel() + gen_letter() + gen_vowel()) ``` More info see in the Python course, lessons about functions. There's more that can be optimized, but I'll save that for later.
21st May 2023, 7:45 PM
Евгений
Евгений - avatar
+ 6
Use just if for the first conditional If you have to make more checks of the same variable, use elif Example: your code takes a number and it checks your age. age = int(input()) if age < 18: #Always use if for the first clause print(“You are not allowed to drive”) elif age >= 80: #Here I’m using elif because I’m checking the same variable print(“You have so much age to drive!”)
20th May 2023, 1:38 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 5
Podle Playz, You are trying to do something that is wrong. Your code was a logical error. When you are trying to print the output (“The bot said: (random letters)”) you are concatenaring a string datatype with an int datatype, and that throws an error. For example: what is the result of “1” + “1” -> “11” What is the result of “1” + 1? -> ?!? Error. You are trying to add a string and a number. What do you want to do is to asign to the variable “alphabet” the value of a random letter. But instead of doing that, you are printing the result of the letter value of the number. So the variable alphabet still contains its initial number. The same happens with vowel and alphabet2. Also, It is a great habit to put if-elif clauses instead of just if clauses So your code should look like this: https://code.sololearn.com/cWuV4SNW28Jw/?ref=app
20th May 2023, 1:32 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 5
Podle Playz oh sorry! I forgot to put if instead of elif in the vowel clause. Now it is fine ;) Pd: the bot said me “die” 😟
20th May 2023, 1:34 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 4
The error is telling you exactly what's wrong. Here's a hint if you still don't understand: ints and strs are not the same thing and you cannot join an int to a str. Think about something you should do if you would like to include a number(int) into a text(str). Maybe...change something?
20th May 2023, 11:55 AM
Justice
Justice - avatar
+ 4
Justice Other error was that he was printing the result of checking alphabet’s value, instead of asign the letter value to this variable. So with a conversion it would look like this: The bot said: 12221 12: alphabet 2: vowel 21: alphabet2 There were too many logical errors 😅 Podle Playz I recommend that you finish conditional lesson group in python course
20th May 2023, 1:41 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 4
Podle Playz Inside the basic python course, the third or fourth group
20th May 2023, 1:43 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 4
Justice Oof, You’re right. So there is another logical error…😅 The alphabet random numbers are from 1 to 6, not from 1 to 26…
20th May 2023, 1:44 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 4
Justice I understand, sorry for my answer.
20th May 2023, 1:49 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 4
Or something like that
20th May 2023, 1:56 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 4
Podle Playz If you're not new to Python, I implore you to the learn how to read errors messages. You can use this code for some pointers or the video I also linked below which has the exact problem you were having. https://code.sololearn.com/W0uW3Wks8UBk/?ref=app https://youtu.be/3p3p6kp39to
20th May 2023, 1:56 PM
Justice
Justice - avatar
+ 4
Read Justice comment
20th May 2023, 1:57 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 3
Just as a note but I wasn't trying to give a full blown answer and completely change your code like Ugulberto Sánchez (though it's still a valueable option). I was trying to usher you to the direction of type conversion. Whereas with with Ugulberto's code, you would have to change half of the lines in your code, if you merely do type conversion, you only would have had to change the very last print statement. Though still great point about if-elif-else.
20th May 2023, 1:37 PM
Justice
Justice - avatar
+ 3
That makes so much more sense now! Thank you, I am actually in the making of an ai so this will help a lot. Thanks!
20th May 2023, 1:39 PM
Podle Playz
Podle Playz - avatar
+ 3
Ugulberto Sánchez Yup! LOL But I would rather guide them on a different approach rather than give a full blown answer but it was still a good example nonetheless! Assignmnts are important to learn as well. Even moreso than typw conversion at such an early stage.
20th May 2023, 1:49 PM
Justice
Justice - avatar
+ 3
No, flow charts
20th May 2023, 1:56 PM
Ugulberto Sánchez
Ugulberto Sánchez - avatar
+ 3
Podle Playz you could read the error description and then you would see that the numbers cannot concatenate with a string. For this reason you have to first convert the numbers into string. You can do that with python inbuilds method: str(). After that the print statement will see as follows: print("Bot said:"+str(alphabet)+str(vowel)+str(alphabet2))
20th May 2023, 10:48 PM
JaScript
JaScript - avatar
+ 2
Yes there are. Every single time you declare a variable in your code, you are assigning them numbers(ints). You are even using those numbers to determine what to print.
20th May 2023, 11:58 AM
Justice
Justice - avatar
+ 2
I already gave you a hint in my initial answer.
20th May 2023, 12:01 PM
Justice
Justice - avatar