Jump to content

Python question - Basic.

X1XNobleX1X

Hello forums,

I'm learning Python, just wanted a little help with my code: 

 

This is what I have at the moment:

 

word = input("Enter the word:")
length = int(input("Enter the repeat length:"))
count = int(input("Enter the repeat count:"))
length = word [-length:]
rest = word [:-count]
print (word+rest*count)
 
At the moment it produces: 
Enter the word: apple
Enter the repeat length: 3
Enter the repeat count: 4
 apple a a a a
 
 
The program has to produce this:
 
Enter the word: banana
Enter the repeat length: 2
Enter the repeat count: 3
banananana
 
or:
Enter the word: apple
Enter the repeat length: 3
Enter the repeat count: 4
appleplepleple

 

So writing a program that reads the three lines of input. The first line is the word, than a number of characters to repeat, than a number of repeats. 

 

Thanks!

|CPU: Intel 5960X|MOBO:Rampage V Extreme|GPU:EVGA 980Ti SC 2 - Way SLI|RAM:G-Skill 32GB|CASE:900D|PSU:CorsairAX1200i|DISPLAY :Dell U2412M X3|SSD Intel 750 400GB, 2X Samsung 850 Pro|

Peripherals : | MOUSE : Logitech G602 | KEYBOARD: K70 RGB (Cherry MX Brown) | NAS: Synology DS1515+  - WD RED 3TB X 5|ROUTER: AC68U

Sound : | HEADPHONES: Sennheiser HD800 SPEAKERS: B&W CM9 (Front floorstanding) ,  B&W CM Center 2 (Centre) | AV RECEIVER : Denon 3806 | MY X99 BUILD LOG!

 

Link to comment
Share on other sites

Link to post
Share on other sites

I don't see a need for the variable rest, but I included both a solution with it as well as one without. Also, you redefine the variable length to be a string after it was an int. Not sure if it was intended, but that will force a specific order if you want to keep the code as close to the original as possible.

word = input("Enter the word:")length = int(input("Enter the repeat length:"))count = int(input("Enter the repeat count:"))rest = word[:-length]length = word[-length:]print(rest + length*count) 
word = input("Enter the word:")length = int(input("Enter the repeat length:"))count = int(input("Enter the repeat count:"))length = word[-length:]print(word + length*(count-1)) 

CPU - FX 8320 @ 4.8 GHz

| MoBo - Sabertooth 990FX | GPU - XFX Radeon HD 7870 GHz Ed. @ 1.075 GHz | CPU Cooler - H100 | RAM - 16 GB Dual Channel Vengeance @ 1600 MHz (didn't care to push these...) | OS - Windows 8 Pro | Storage - OCZ Vertex 3 (120 GB Boot), Samsung 830 Pro 64 GB, WD Black 1 TB, some random 320 GB from a laptop | PSU - CM Silent Hybrid Pro 850W (MDPC Sleeving) | Case - 800D | Monitors - ASUS V238H/ X Star DP2710LED | Mouse - M90 Keyboard - CM Quickfire Rapid w/ custom key caps

"When life gives you lemons, Don't make lemonade, make life take the lemons back!" - Cave Johnson, CEO

Link to comment
Share on other sites

Link to post
Share on other sites

word = input("Enter the word: ");length = int(input("Enter the repeat length: "));count = int(input("Enter the repeat count: "));rest = word[:-length]; # the rest of the wordend = word[-length:]; # the part to the repeatedprint(rest+end*count);
Link to comment
Share on other sites

Link to post
Share on other sites

word = input("Enter the word: ");length = int(input("Enter the repeat length: "));count = int(input("Enter the repeat count: "));rest = word[:-length]; # the rest of the wordend = word[-length:]; # the part to the repeatedprint(rest+end*count);

That worked.

You're amazing!

Thank you!

|CPU: Intel 5960X|MOBO:Rampage V Extreme|GPU:EVGA 980Ti SC 2 - Way SLI|RAM:G-Skill 32GB|CASE:900D|PSU:CorsairAX1200i|DISPLAY :Dell U2412M X3|SSD Intel 750 400GB, 2X Samsung 850 Pro|

Peripherals : | MOUSE : Logitech G602 | KEYBOARD: K70 RGB (Cherry MX Brown) | NAS: Synology DS1515+  - WD RED 3TB X 5|ROUTER: AC68U

Sound : | HEADPHONES: Sennheiser HD800 SPEAKERS: B&W CM9 (Front floorstanding) ,  B&W CM Center 2 (Centre) | AV RECEIVER : Denon 3806 | MY X99 BUILD LOG!

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×