+ 2

Confused on what the error is in my code

random = 'abc sjfiwb' random[3:6] print(random[2:7]) print(input("enter a number: ")) * (input("enter a word: ")) I was practicing what I learned and I was trying to multiply a word entered by the number entered, so it would repeat the word that amount of times. what am I doing wrong? I tried changing it over and over to no avail

3rd Apr 2017, 2:08 PM
Abbie Ruperto
Abbie Ruperto - avatar
7 odpowiedzi
+ 2
well I did put int at the beginning, so I'm confused why the format I used doesn't work?
3rd Apr 2017, 3:19 PM
Abbie Ruperto
Abbie Ruperto - avatar
+ 2
I just used word and num as variables. input() ... this takes input as a string int(input()) ... this takes input as an integer. word=input() gives word the value of whatever word(s) you give it num=int(input()) gives num the value of whatever number you give it. same as you did above with your first line. you made a variable called random and gave it a value of abc sjfiwb. that's all num is. just what I named a variable
3rd Apr 2017, 3:24 PM
LordHill
LordHill - avatar
+ 2
@Eranga what's the extra parenthesis at then end for? tried it in my code and that got an error so that don't know how it worked in that one
3rd Apr 2017, 3:29 PM
Abbie Ruperto
Abbie Ruperto - avatar
+ 1
print(input("enter a word: ") * int(input("enter a number: "))) inputs by default are strings. if you want one input to be a number, cast it as an integer. to explain it in an easier to see way, here. word=input("Enter a word") num=int(input("Enter a number")) print(word*num)
3rd Apr 2017, 2:36 PM
LordHill
LordHill - avatar
+ 1
the format you used will work. if you have your input wrapped inside of int() it will. you do have a second ) after your number input however
3rd Apr 2017, 3:53 PM
LordHill
LordHill - avatar
+ 1
Let me break down ur last line. print(input("enter a number: ")) * (input("enter a word: ")) Below I have separated the code from * in to 2 parts 1=> print(input("enter a number: ")) 2=> (input("enter a word: ")) 1=> gets user input and print it 2=> just get the users input so, 1*2=> u trying to multyply these two. I think now u understand there is something very wrong here. Correct Code: print(int(input("enter a number: ")) * (input("enter a word: "))) I will do the same.before that let me omit the print statement. it will look like below int(input("enter a number: ")) * (input("enter a word: ")) 1=> int(input("enter a number: ")) 2=> (input("enter a word: ")) 1=> get user input and convert it into an integer 2=> get user input​ which is a string let's say user entered number 2 and "word" as word 1*2 => 2 * "word" result => wordword now u want to print this so, print(1*2) => print(int(input("enter a number: ")) * (input("enter a word: "))) I thing u r referring to open and close paranthacis of 2nd statement (input("enter a word:")). paranthacis used for grouping but here u can avoid it.it will be input("enter a word:") If I clean up this code little bit. I'm using variables here nTimes,myWord.it helps to break our statements and clear code for better understanding. nTimes = int(input("enter a number: ") myWord = input("enter a word: ") print(nTimes * myWord)
3rd Apr 2017, 4:25 PM
Eranga
Eranga - avatar