Where did zero come from? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

Where did zero come from?

import random n = input(int()) list = [0, "1-red", "2-black", "3-red", "4-black", "5-red", "6-black", "7-red", "8-black", "9-red", "10-black"] r = int(random.randint(0,10)) print(list[r]) print(n) Why does this code output zero at the beginning? https://code.sololearn.com/cf53hNUlcfew/?ref=app

6th May 2023, 7:29 PM
Tayoz_kitayoz
4 Réponses
+ 5
Zero comes from India. Your weird output comes from doing input(int()) instead of int(input()).
6th May 2023, 7:39 PM
Orin Cook
Orin Cook - avatar
+ 3
Also, never use existing function or class names, like list, for your variables. This overloads the existing resource, and you can get weird and hard to follow problems.
8th May 2023, 3:34 AM
Emerson Prado
Emerson Prado - avatar
+ 2
More specifically, int() returns 0 by itself, and arg in input(arg) prints arg as a prompt before taking input, but you can't really see that happening in the playground because all input is entered at the beginning
6th May 2023, 7:43 PM
Orin Cook
Orin Cook - avatar
+ 1
Orin Cook good catch.😎 input() displays anything inside the () as a prompt. int() is 0, so you have a 0 stuck in your display with n=input(int()). The print(list[r]) is displayed beside that, because there is no '\n' in your input prompt. So you end up with 01-red or something similar. Tayoz_kitayoz you can omit some things to simplify your code. list = ["1-red", "2-black", "3-red", "4-black", "5-red", "6-black", "7-red", "8-black", "9-red", "10-black"] r = random.randint(0,9) also, list is a bad variable name
7th May 2023, 1:01 AM
Bob_Li
Bob_Li - avatar