Python Forum
Newbie here. What's wrong with my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie here. What's wrong with my code?
#1
Hey Guys, I'm new to Python and Coding stuff. I just started using Python for my course. So I decided to See a few Tutorials but for this particular code I can't seem to make it work. Please Help Me.

This is the code:

print (("%s is a very #s person. He usually spend all his time #s.") % (input("Please enter a name: ")), (input("Please enter adjective: ")), (input("Please enter 'ing' verb: ")))
This is my result:

Output:
Please enter a name: Bill Please enter adjective: Deranged Please enter 'ing' verb: Killing Bill is a very #s person. He usually spend all his time #s. Deranged Killing
As you can see, the #s is still visible and it did not substitute with both Adjective and Ing.

Note: I have edited some of the code since the OP who make that tutorial using Python 2. This is the closest result I can get.

Original Code:

print (("%s is a very %s person. They usually spend all their rime %s.") % input ("Please enter a name: "), input ("Please enter an adjective: "), input ("Please enter an 'ing' verb: "))
Reply
#2
The code that you call "Original' is actually python3 code, not python2 code.
Old-style string formatting, that you use here is using % as a placeholder, not #. # is simply char as any other, without special meaning and is printed as is.
it is recommended that you use new-style formatting.
print('{} is my name'.format('GalaxyCR'))
Reply
#3
Thanks buran for the help and the edit. I'll try to improve and fix it.
Reply
#4
btw, I found the tutorial on youtube and the guy there is using python2 indeed (e.g. using raw_input, not input). So the 'original code' is in fact not that 'original' one...
also, in python3.6. there is also so called formatted string literals or simply f-strings:
name = "GalaxyCR"
print( f"He said his name is {name}.")
PEP498 for detailed discussion
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 471 Nov-07-2023, 04:32 PM
Last Post: snippsat
  newbie question - can't make code work tronic72 2 674 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Something wrong with my code FabianPruitt 5 843 Jul-03-2023, 10:55 PM
Last Post: Pedroski55
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,551 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,243 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,770 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,518 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,516 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,624 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Newbie - code solution explained Stjude1982 2 1,826 Sep-16-2021, 08:54 AM
Last Post: Stjude1982

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020