+ 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
4 Réponses
+ 5
Zero comes from India. Your weird output comes from doing input(int()) instead of int(input()).
+ 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.
+ 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
+ 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