+ 4
python programming syntax error
I am writing a code that lets the computer choose a random number between 0 and 100. The user then has to input a number and try to guess the random number. The computer should either say too low , too high or correct but there is a syntax error and I don't know how to solve it so that the code can work. Does anyone know how to get the code to work? Thanks 😊🌺 https://code.sololearn.com/cFqRgblj9GiR/?ref=app
7 Respuestas
+ 9
You can write your if statements in one line as in line 5, but as soon as the next line isn't indented anymore, the if statement is done and you can't use elif. It's better to write them the regular way:
if condition:
do_something()
elif (...)
Also, your if statements have to be indented as they are part of the while loop. And you need to import random
+ 7
You have to structure your code better.
- put each statement in a separate line
- use the correct indentation following the "while" and "if" statements, this is very important to define the control flow
You need to import the random library in the beginning otherwise you cannot use the randint function.
Also even if you fix the syntax errors, this type of code won't really run well on SoloLearn because it requires repeated interactive inputs from the user... and sadly the sololearn interpreter can handle only a single batch of inputs at the beginning... then it will process the code. so your program will give you an EOFError (meaning not enough inputs were provided).
You can test it better on another web-based platform e.g.
https://repl.it/languages/python3
or on your own computer
+ 5
Do you use Ubuntu? Usually you can just open a terminal and run python by typing "python" and return. If python isn't installed, try "sudo apt-get install python".
+ 4
I succeded , I will upload the new code shortly 💃🍻
Thank you all !
+ 4
alice boulay You can use GCC/g++ to code with C/C++ in Linux. It can be installed with sudo apt-get install g++. I think it is preinstalled in Ubuntu
+ 3
Thanks🌺 but I can't compile it on my computer ( linux system ). Apparently it should already have a python interpreter but I can't find it.
+ 3
Anna Thanks for your advice 🍵
Can we do the same for cpp?