I am trying to program a short story line as part of my scripting class, but I have been getting a weird result.
Work so far:
Values
first_name = input ('Nick')
generic_location = input ('Costco')
whole_number = input ('12')
plural_noun = input ('donuts')
print(first_name, 'went to', generic_location, 'to buy', whole_number, 'different types of', plural_noun)
What end result SHOULD be: Nick went to Costco to buy 12 different types ofdonuts.
Actual result: NickCostco12donutsNick went to Costco to buy 12 different types of donuts.
Where did I go wrong?
CodePudding user response:
I think that your code have some problems. Firstly, the "input()" means that you are asking the user for prompt for the variable. I think that you want to assign a value to the variable, but instead you used an "input()" with the value inside. "input()" means that Python gonna ask you for prompt/answer asking the value inside. If you want to assign the variables and not asking user answer, you can assign the variables like this:
first_name = 'Nick'
generic_location = 'Costco'
whole_number = '12'
plural_noun = 'donuts'
print(first_name, 'went to', generic_location, 'to buy', whole_number, 'different types of', plural_noun)
Other than that, I don't see any problems with your code, I tried running it on Python 3.10 and see no problems with it. If you still have problems, I think you can try out f strings, which looks like this.
print(f"{first_name} went to {generic_location} to buy {whole_number} different types of {plural_noun}")
CodePudding user response:
You are making it harder than it should be.
First of all, your question is likely to be removed if you don't appropriately format your code snippets. You should wrap them in "```", and they will format automatically.
Regarding your problem, you are using "input" function the wrong way. This function reads a line of input from your standard input. You can read more about it in Python Documentation.
For this exercise, you only need to do basic variable assignment, something like:
first_name = "Nick"
generic_location = "Costco"
whole_number = "12"
plural_noun = "donuts"
print(first_name, "went to", generic_location, "to buy", whole_number, "different types of", plural_noun)
Good luck with your classes!