473,382 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

I need help with a loop that replaces a number in a sentence

Hi,

If I have a sentence like this:

"I now have a total of % yellow bananas!"

but I need it to change the number 2 to a 3, print, then a 4, print, etc until it reaches a set number like 10, how can I go about that?

Expand|Select|Wrap|Line Numbers
  1.  
  2. sentence = "I now have a total of % yellow bananas!"
  3.  
  4. for i in range(2,11):
  5.     print sentence %i
  6.  
  7.  
but that gets me an error:
ValueError: unsupported format character 'y' (0x79) at index 9

When I try it with a different sentence:
Expand|Select|Wrap|Line Numbers
  1. sentence = " I have % dogs!"
  2. for i in range(2,11):
  3.     print sentence %i
  4.  
...it almost works:
I have 2ogs!
I have 3ogs!
I have 4ogs!
I have 5ogs!
I have 6ogs!
I have 7ogs!
I have 8ogs!
I have 9ogs!
I have 10ogs!
Sep 5 '14 #1
1 1314
bvdet
2,851 Expert Mod 2GB
Python is interpreting the "d" in "% d" as a string formatting type specifier. Add an 's' or other appropriate specifier.
Expand|Select|Wrap|Line Numbers
  1. >>> sentence = " I have %s dogs!"
  2. >>> for i in range(2,11):
  3. ...     print sentence % i
  4. ...     
  5.  I have 2 dogs!
  6.  I have 3 dogs!
  7.  I have 4 dogs!
  8.  I have 5 dogs!
  9.  I have 6 dogs!
  10.  I have 7 dogs!
  11.  I have 8 dogs!
  12.  I have 9 dogs!
  13.  I have 10 dogs!
  14. >>>
Sep 6 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Geoff Lane | last post by:
Browser: IE5+ I have a form with an unknown number of checkboxes (the page is generated by a server-side script). I need to loop through those checkboxes, test whether the name of each checkbox...
4
by: dovelet | last post by:
Hi all, I am new in javascript and I hope someone can help me. I would like to write a web page with 2 frame, left and right. When browse the web page, it will read a file from the hard disk....
8
by: Kappadon5 | last post by:
Hello All, I am new to programming and I apologize in advance if I am out of protocol in any way shape or fashion. My problem is that I have a program where you select an option from two different...
2
by: chalrav | last post by:
Hi, I have three tables as below: Table: Demand ITEM_ID, QUANTITY Item A, 10 Table: Route ITEM_ID, OPERATION_NO, RESOURCE, TIME
5
by: jdvon | last post by:
So I have downloaded this neat expression for a textbox control in a form that generates automatic sequence numbers with the 2-digit year in front of it: =Format(Now(),"yy") & "CAR" & "-" &...
17
by: scan87 | last post by:
Can somone please, please give me the solution for the following problem. I need to submit it on Monday. Write a global function called format, which formats numbers with a given number of decimal...
2
by: QHorizon | last post by:
Hello, I'm new to Python (I've learned everything up to iterators so far) and fairly new to Programming. This would be my first real program: #Coordinate Geometry (The whole program is not...
8
by: perlhelp | last post by:
Hi everyone, I am relatively new to perl. I am trying to write a script that can access directories that have variable numbers appended to the end. For example, I have 4 directories that all have an...
2
Thekid
by: Thekid | last post by:
I had made a post about making a loop using letters instead of numbers and dshimer gave me this solution: for i in range(65,70): for j in range(65,70): for k in range(65,70): ...
3
by: Gulo | last post by:
Hello, I need to extract the number of google plus likes of some pages, for example bytes.com the number is here: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.