How to get time in MM:SS format? (python3) - python-3.x

I'm trying to write a program that takes a certain time in MM:SS format, but whenever I try this:
a = input("Enter time here: ")
I get:
Traceback (most recent call last):
File "X", line 26, in <module>
if switch == False and (int(inpt[i])>=0 or int(inpt[i]) <= 9):
ValueError: invalid literal for int() with base 10: ':'
Am I supposed to use the datetime module to make this work? If so, how?
Thanks in advance :)

Related

How do I fix this EOF error on python 3 version

I am working on a very basic problem on Hackerrank.
Input format:
First line contains integer N.
Second line contains string S.
Output format:
First line should contain N x 2.
Second line should contain the same string S.
sample test case
5
helloworld
my code is as: (on PYTHON 3)
n=int(input())
s=input()
print(2*n)
print(s)
I am getting error:
Execution failed.
EOFError : EOF when reading a line
Stack Trace:
Traceback (most recent call last):
File "/tmp/143981299/user_code.py", line 1, in <module>
N = int(input())
EOFError: EOF when reading a line
I tried this method to take input many times and this is the first time I am having this error. Can anyone please explain why?
use a try/except block to handle the error
while True:
try:
n=int(input())
s=input()
print(2*n)
print(s)
except:
break

python3 cant convert string to datetime object

I have the following code:
from datetime import datetime
date_time_str = '2020-07-17 21:59:49.55'
date_time_obj = datetime.strptime(date_time_str, '%y-%m-%d %H:%M:%S.%f')
print "The type of the date is now", type(date_time_obj)
print "The date is", date_time_obj
Which results in the err:
Traceback (most recent call last):
File "main.py", line 5, in <module>
date_time_obj = datetime.strptime(date_time_str, '%y-%m-%d %H:%M:%S.%f')
File "/usr/lib/python2.7/_strptime.py", line 332, in _strptime
(data_string, format))
ValueError: time data '2020-07-17 21:59:49.553' does not match format '%y-%m-%d %H:%M:%S.%f'
Why cant I convert this date? The following example works:
date_time_str = '18/09/19 01:55:19'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
First of all, this is not valid Python 3 code. You've used the Python 2 print statement in your code, and trying to run this on Python 3 causes a SyntaxError.
As the error indicates, your date string does not match the format you specified. Take a look at the format codes; the first issue I notice is that you give a 4-digit year (2020) but try to line it up with %y, which is for two-digit year. There may be other issues as well, which should be easy to find looking through that table.

I am trying to get the user input in python

This is my code but I am getting an error while compiling in python.
Code
print ("Enter the height :")
feet = int(input("feet:"))
Error
Enter the height :
Traceback (most recent call last):
File "main.py", line 2, in <module>
feet = int(raw_input("feet:"))
NameError: name 'raw_input' is not defined
raw_input is no longer a part of python 3.x
You can use
feet = int(input("Enter height in feet: "))
print(feet)

Trouble using exponents in python 3.6 [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
I'm trying to create a script that allows users to input numbers and then the numbers are used for to create an exponent. Here's what i'm writing. I'm in python 3.6 and using anaconda/spyder.
print ("Enter number x: ")
x = input()
print ("Enter number y: ")
y = input()
import numpy
numpy.exp2([x,y])
so what I want is for the user to enter a value for x. for example 2. then enter a value for y. for example 3. then i'd like (with the help of numpy) create the exponent 2**3 which equals 8. instead i get this.
Enter number x:
2
Enter number y:
3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/Users/Ameen/Desktop/assignment1.py", line 16, in <module>
numpy.exp2([x,y])
TypeError: ufunc 'exp2' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
I tried print ('xy') but that printed xy. so i came across this site https://docs.scipy.org/doc/numpy/reference/generated/numpy.exp2.html#numpy.exp2 they suggested np.exp2. when I tried np.exp2([x**y]) it says np is not defined and an error message saying the numpy library is not imported. so now i'm trying numpy.exp2 and got the error above.
Convert strings to integers:
import numpy
print('Enter number x:')
x = int(input())
print('Enter number y:')
y = int(input())
print(numpy.exp2([x, y])) #=> [ 4. 8.]

%s error when trying to put a variable into a string python

Trying to build a "launcher" type thing for youtube-dl(program that lets you download youtube videos), and getting an error. I understand what the error means but it makes no sense as I am(I think) doing exactly what python wants.
This is my code:
import os
import sys
x = input('Enter link for youtube Video you would like to download: ')
ytdp = open('C:\\Games\\ytdp.bat', 'w')
ytdp.write('cd C:\\Users\Jake\Music')
ytdp.write('\n')
ytdp.write('youtube-dl %s')% (x)
ytdp.close()
os.startfile('C:\\Games\ytdp.bat')
sys.exit()
This is the error I get when running the code
Traceback (most recent call last):
File "C:\Users\Jake\Desktop\Youtube video downloader.py", line 8, in <module>
ytdp.write('youtube-dl %s')% (xx)
TypeError: unsupported operand type(s) for %: 'int' and 'str'
ytdp.write('youtube-dl %s')% (xx)
Should be
ytdp.write('youtube-dl %s'% (xx))
Here is some more info on it

Resources